Android: Fragments backStack

100,742

Solution 1

If you really want to replace the fragment then use replace() methode instead of doing a remove() and an add().

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(..............);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit(); 

Don't forget to do the addToBackStack(null) so your previous state will be added to the backstack allowing you to go back with the back button.

See also https://developer.android.com/reference/android/app/FragmentTransaction.html#replace(int, android.app.Fragment, java.lang.String) .

Another good source is http://developer.android.com/guide/components/fragments.html (search for replace() function).

Solution 2

Just remove first and the call super.onBackPressed

public void onBackPressed(){

    // here remove code for your last fragment
    super.onBackPressed();

}
Share:
100,742
Oritm
Author by

Oritm

Making my life easier by developing: APPS iOS (Swift/Objective C) Android (Java) BACKEND SOFTWARE NodeJS (Typescript) Postgres WEBSITES Angular (Typescript) I'm also a little interested in: Python JQuery PHP

Updated on July 30, 2022

Comments

  • Oritm
    Oritm almost 2 years

    Im trying to load an new fragment when a method is called. This method creates a new fragment and "replaces" the other fragment:

    private void showTestFragment(Fragment oldFragment, boolean addBackStack, BaseAdapter adapter, int position) {
        Cursor cursor = (Cursor)adapter.getItem(position);
        if(cursor != null){
    
            int idx = cursor.getColumnIndexOrThrow(Episode._ID);
            long rowId = cursor.getLong(idx);
    
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    
            if(oldFragment != null){
                Log.i(TAG, "Removing the old fragment");
                fragmentTransaction.remove(oldFragment);
            }
    
            TestFragment testFragment =  new TestFragment();
            testFragment.setId(rowId);
    
            fragmentTransaction.add(android.R.id.content, testFragment);
    
            if(addBackStack){
                Log.i(TAG, "Added to the backstack");
                fragmentTransaction.addToBackStack(TAG);
            }
    
            fragmentTransaction.commit();
            Fragment f = getFragmentManager()
                    .findFragmentById(R.id.index);
            Log.i(TAG, "after commit, frag is "+ f);
        }
    }
    

    This works fine, until i go back. The last fragment, should be removed when i go back. Before i'm going to implement methods on the activities

    public void onBackPressed(){}
    

    to remove the last fragment, i want to know if i handle the fragment change correctly. It looks like i'm missing something here..

  • Zar E Ahmer
    Zar E Ahmer about 10 years
    replace() == remove(thisFragment).add(anotherFragment).. Replace is equal to both the methods in this order.
  • ToolmakerSteve
    ToolmakerSteve over 7 years
    @Nepster, true, but not always convenient. FWIW, The difference is that Replace removes ANY fragment(s) in the container; don't need to know thisFragment. One scenario where this matters is if Fragment A links to Fragment B1 which is replaced by Fragment B2. THEN if want "Back" button to go back to Fragment A (instead of to B1), we have to code that manually, because Android's back will [incorrectly IMHO] do "remove B1, add A" - the opposite of the original link from A to B1. B2 lurks in viewbehind. Our code has to know that B2 is the current fragment, not B1. Sometimes that is inconvenient.