Im working with an android app which have 3 fragments and the activity_main, all of them are listed in a navigation drawer. The question is how can i return to the activity main by clicking in a item of the navigation drawer.
The basic idea of waht I need is to close all the fragments open to return to the activity main
This is my code.
if(id==R.id.taximetro){
//this is my activity_main that i need to return
trans = true;
} else if (id == R.id.factura) {
fragment=new frmFactura();
getSupportFragmentManager().beginTransaction().replace(R.id.content_main, fragment).addToBackStack(null).commit();
trans = true;
} else if (id == R.id.dispBlue) {
fragment=new frmDispositivos();
getSupportFragmentManager().beginTransaction().replace(R.id.content_main, fragment).addToBackStack(null).commit();
trans = true;
} else if (id == R.id.ayuda) {
fragment=new frmAyuda();
getSupportFragmentManager().beginTransaction().replace(R.id.content_main, fragment).addToBackStack(null).commit();
trans = true;
}
if (trans) {
getSupportFragmentManager().beginTransaction().replace(R.id.content_main, fragment).commit();
item.setCheckable(true);
getSupportActionBar().setTitle(item.getTitle());
}
//getSupportFragmentManager().beginTransaction().replace(R.id.content_main, fragment).addToBackStack(null).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Simple like you call fragments ..call your MainActivity
if you want check whats available on the view,
keep an ENUM value or other related way ..and change the value related the the view that you loaded ,and check a fragment is loaded or still the main view exists in your MainActivity
using that ENUM value in your MainActivity call
so,
if a fragment view is loaded recall your MainActivity
else just slide the navigation because you are already in the view of MainActivity without any fragment loaded on it
if(id==R.id.taximetro){
// call your activity again if you want you can check that a fragment view is loaded or not and do changes only when you need if there is a fragment view loaded.. or simply recall it
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.