我正在使用具有3个片段和activity_main的Android应用程序,所有这些都列在导航抽屉中。问题是如何通过单击导航抽屉的项目返回活动主页。
我需要的基本思路是关闭所有打开的片段以返回活动主
这是我的代码。
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;
}
答案 0 :(得分:0)
就像你调用片段一样简单..拨打你的MainActivity
如果您想查看视图中的可用内容,
保持ENUM值或其他相关方式..并更改与您加载的视图相关的值,并检查片段是否已加载,或者MainActivity
中的主视图是否存在于您使用的ENUM值中MainActivity调用
所以,
如果加载了片段视图,请调用MainActivity
否则只需滑动导航,因为您已经在MainActivity视图中而没有加载任何片段
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();
}