我用帶片段的導航抽屜製作項目,導航有3個片段。我有問題,當我在第三個片段,我按下應用程序突然關閉按鈕,但我想要做的是從第三個片段到第一個片段的片段更改。我該怎麼做?如何處理返回按鈕以轉到特定片段?
這是我的MainActivity代碼:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
DrawerLayout drawer;
NavigationView navigationView;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
Fragment fragment = null;
SessionManager session;
TextView kode_kt, nama_ketua;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
session = new SessionManager(getApplicationContext());
session.checkLogin();
fragmentManager = getFragmentManager();
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header=navigationView.getHeaderView(0);
kode_kt = (TextView)header.findViewById(R.id.username);
nama_ketua = (TextView)header.findViewById(R.id.email);
HashMap<String, String> user = session.getUserDetails();
kode_kt.setText(user.get(SessionManager.KEY_KODE));
nama_ketua.setText(user.get(SessionManager.KEY_KETUA));
// email.setText("Jopa Software House");
// tampilan default awal ketika aplikasii dijalankan
if (savedInstanceState == null) {
fragment = new Monitoring();
callFragment(fragment);
}
}
@Override
public void onBackPressed() {
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Toast.makeText(getApplicationContext(), "Action Settings", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
// Untuk memanggil layout dari menu yang dipilih
if (id == R.id.nav_camera) {
fragment = new Monitoring();
callFragment(fragment);
} else if (id == R.id.nav_gallery) {
showDialog();
} else if (id == R.id.nav_send) {
fragment = new Akun();
callFragment(fragment);
}
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
// untuk mengganti isi kontainer menu yang dipiih
private void callFragment(Fragment fragment) {
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragment);
fragmentTransaction.replace(R.id.frame_container, fragment);
fragmentTransaction.commit();
}
private void showDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
// set title dialog
alertDialogBuilder.setTitle("Keluar Akun");
// set pesan dari dialog
alertDialogBuilder
.setMessage("Anda ingin keluar dari akun?")
.setIcon(R.mipmap.ic_launcher)
.setCancelable(false)
.setPositiveButton("Ya",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// jika tombol diklik, maka akan menutup activity ini
session.logoutUser();
}
})
.setNegativeButton("Tidak",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// jika tombol ini diklik, akan menutup dialog
// dan tidak terjadi apa2
dialog.cancel();
}
});
// membuat alert dialog dari builder
AlertDialog alertDialog = alertDialogBuilder.create();
// menampilkan alert dialog
alertDialog.show();
}
}
做應用程序關閉或在logcat中拋出錯誤崩潰? –