简体   繁体   English

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

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

Im working with an android app which have 3 fragments and the activity_main, all of them are listed in a navigation drawer. 我正在使用一个具有3个片段和activity_main的android应用程序,它们都在导航抽屉中列出。 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 就像调用片段一样简单..调用您的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 保持ENUM值或其他相关方式..并更改与您加载的视图相关的值,并检查是否已加载片段,或者在MainActivity使用MainActivity调用中的ENUM值在MainActivity中仍然存在主视图

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 如果加载了片段视图,请回想您的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