繁体   English   中英

通过单击导航抽屉中的某个项目返回活动主界面

[英]Return to the activity main by clicking in a item of the navigation drawer

我正在使用一个具有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;
}

就像调用片段一样简单..调用您的MainActivity

如果您想查看视图中可用的内容,

保持ENUM值或其他相关方式..并更改与您加载的视图相关的值,并检查是否已加载片段,或者在MainActivity使用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();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM