Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Fragments backStack

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..

like image 851
Oritm Avatar asked Jan 16 '13 09:01

Oritm


People also ask

What is Backstack in fragments?

Calling addToBackStack() commits the transaction to the back stack. The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.

How do I get Backstack fragments?

You can use the getName() method of FragmentManager. BackStackEntry which was introduced in API level 14. This method will return a tag which was the one you used when you added the Fragment to the backstack with addTobackStack(tag) . Show activity on this post.

How can I maintain fragment state when added to the back stack?

add() - (create and) add a Fragment B and it overlap Fragment A, which is still active in the background. remove() - can be used to remove Fragment B and return to A. Fragment B will be recreated when called later on.


2 Answers

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).

like image 129
K Roobroeck Avatar answered Oct 12 '22 18:10

K Roobroeck


Just remove first and the call super.onBackPressed

public void onBackPressed(){      // here remove code for your last fragment     super.onBackPressed();  } 
like image 34
Pratik Avatar answered Oct 12 '22 19:10

Pratik