0

I am very new to android and try to convert my iphone app to android. I try to nevigate between fragments, and after navigating from fragment A to B, I want to go back from fragment B to A with the back button. I made a research but could not understand it well. Below in the code I have a navigation drawer in which I navigate between fragments. After clicking followers I want to go back to the news feed with the back button.

//this is the navigation drawer part     
if (id == R.id.my_archive_drawer) {

                myArchiveFragment fragment = new myArchiveFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.addToBackStack("newsFeedFragment");
                fragmentTransaction.commit();

            } else if (id == R.id.followers_drawer) {

                followedFollowersFragment fragment = new followedFollowersFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.addToBackStack("newsFeedFragment");
                fragmentTransaction.commit();

            } 
//this is the navigation drawer part   



//this is the on create part part   
        newsFeedFragment fragment = new newsFeedFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
//this is the on create part part  


//onbackpreseed part
   public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            getFragmentManager().popBackStack("newsFeedFragment");
        }
    }
//onbackpressed part
saner
  • 821
  • 2
  • 10
  • 32

1 Answers1

2

The addToBackStack means to put the fragment in the stack. This means that when you push fragment news to fragment followers you are adding to the the stack the fragment. So when you go back you return to news fragment.

Try this code:

followedFollowersFragment fragment = new followedFollowersFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

And then in your onBackPressed() remove the popbackstack(), because when you call the onBackPressed clicking the back button you are already removing the followers fragment. So the onBackPressed() should be like this:

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

For more information about this 2 methods you can read here

JpCrow
  • 4,881
  • 4
  • 32
  • 46
  • Thank you very much JpCrow, it worked:) what if I want to disable the back button when I am in the news_freed fragment that was created in the "on create" part, the starting fragment? – saner Mar 20 '16 at 12:59
  • In that case you should override the onBackPressed() and not calling the super.onBackPressed(); only when is that case, you should add a flag or something – JpCrow Mar 20 '16 at 16:55