简体   繁体   中英

fragment back goes to specified fragment

I am developing an android app using fragments. I have five fragments. Those are A,B,C,D,E . My first fragment A is the main fragment. First I traverse from A to B then from B to C. In C when I press back button I want it to go back to A. I have used the below code for calling B fragment from A. This same method is used for calling another fragments.

FragmentB fragment = new FragmentB();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction().addToBackStack("tag");
transaction.replace(R.id.content_frame, fragment);
transaction.commit();

The back button code in the activity is as following

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }

}

How to solve this issue please help me.

Try this:

@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawers();
        return;
    }

    // This code loads home fragment when back key is pressed
    // when user is in other fragment than home
    if (shouldLoadHomeFragOnBackPress) {
        // checking if user is on other navigation menu
        // rather than home
        if (navItemIndex != 0) {
            navItemIndex = 0;
            CURRENT_TAG = TAG_HOME;
            loadHomeFragment();
            return;
        }
    }

    super.onBackPressed();
}

Refer this reference : https://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

You can try this:

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    }    FragmentManager fragmentManager = getSupportFragmentManager();
    int backCount = fragmentManager.getBackStackEntryCount();
    System.out.println("back count " + backCount);

    if(backCount==0)
    {
        actionBar.setTitle("Home");
       //Here you can show alert dialog for exit the app?
    }
  /*  else if(backCount==1)
    {

    }*/
    else if(backCount==1)
    {
        fragmentManager.popBackStack();
        // setTitle("Home");
        navigationView.getMenu().getItem(0).setChecked(true);
        actionBar.setTitle("Home");
    }
    else if(backCount>1)
    {

        for(int i=fragmentManager.getBackStackEntryCount();i>=1;i--)
        {
            fragmentManager.popBackStack();
            System.out.println("back count loop" + fragmentManager.getBackStackEntryCount());
        }
        actionBar.setTitle("Home");
    }
}

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.

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