14

is it possible to switch between Fragments without re-creating them all the time? If so, how?

In the documentation I found an example of how to replace Fragments.

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

But I don't want to create my Fragments from the scratch every time I need them.

I also found this example of hiding/showing Fragments:

// The content view embeds two fragments; now retrieve them and attach
// their "hide" button.
FragmentManager fm = getFragmentManager();
addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));

But how would I create a fragment with an ID outside an XML file?

I think this might be related to this question, but there isn't an answer. :/

Thank you very much in advance, jellyfish

Edit:

That's how I'm doing it now:

Fragment shown = fragmentManager.findFragmentByTag(shownFragment);

//...

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (shown != null) fragmentTransaction.hide(shown);

//switch statetement for menu selection, just one example:

SettingsFragment set = (SettingsFragment) fragmentManager.findFragmentByTag(SET);
Toast.makeText(this, "Settings:" + set, Toast.LENGTH_LONG).show();
if (set == null)
{
        set = new SettingsFragment();
        fragmentTransaction.add(R.id.framelayout_content, set, SET);
}
else fragmentTransaction.show(set);
shownFragment = SET;
fragmentTransaction.commit();

If I call up the settings, then something else, and then go back to settings, the toast gives me "null" first and "Settings:SettingsFragment{40ef..." second.

However, if I replace fragmentTransaction.add(R.id.framelayout_content, set, SET); with fragmentTransaction.replace(R.id.framelayout_content, set, SET); I keep getting "null", "null", "null"... so it doesn't seem to find the Fragment by tag.

Edit2:

Adding fragmentTransaction.addToBackStack(null); did the trick. :) This saves the whole hiding/memorizing which fragment is shown part so I suppose it's the most elegant solution for this.

I found this tutorial quite helpful on the topic.

Edit3:

Looking at my code I realized I could get rid of some parts, so I changed it to:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (shown != null) fragmentTransaction.hide(shown);
Settings set = (Settings) fragmentManager.findFragmentByTag(SET);
if (set == null) set = new Settings();

fragmentTransaction.replace(R.id.framelayout_content, set, SET);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

However, this invoked an IllegalStateException: Fragment already added, much the same like here. Is there an easy way to prevent this? Otherwise I think I might switch back to the hide/show bit.

Community
  • 1
  • 1
jellyfish
  • 7,868
  • 11
  • 37
  • 49

4 Answers4

5

It could depend on what you are trying to avoid being re-created.

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

In your example example when you hit the back button from your newFragment the previous fragment will be shown (you'll get an onCreateView, onActivityCreated but no onCreate) so that fragment isn't being re-created as such. As for you newFragment you can still keep it around if you plan to use it again updating any internal state as required in say onCreate or onActivityCreated.

EDIT:

If you simply have a menu list with each entry invoking a different fragment in a right pane then adding to the back stack is not what you want. For this you might get away with calling add(...) on each fragment up-front and simply hide/show each fragment as required (I've not tested this). Otherwise I would suggest holding a reference to each fragment, call replace(...) on selecting a different menu item ensuring that you don't add to the back stack.

PJL
  • 18,735
  • 17
  • 71
  • 68
  • I want to switch between Fragments depending on the menu selection. So I want to re-show the same Fragment again if you are selecting the same menu item again, too. With "replace" I couldn't find the Fragment via the tag anymore. – jellyfish Jun 01 '11 at 08:37
  • 1
    Then I would simply hold onto the fragments in memory and do the replace. I don't see why you need to find the fragment given that you can track the current menu selection and know what fragment is being displayed. However, when a new menu item is selected then I don't see why finding the fragment by tag shouldn't work (I've done something similar with a DialogFragment as does, if memory serves, the DialogFragment samples in API demos. – PJL Jun 01 '11 at 19:55
  • So, are you suggesting that I should keep a reference field in my class for each Fragment? For the other part, please see my edit. – jellyfish Jun 06 '11 at 08:51
  • 1
    I'm finding the show/hide mechanism a little confusing. If I'm not wrong in Edit3 you can find a non-null 'set' fragment which is part of the activity state and then call replace(...) with it so you would get the exception as it's already added. I think adding to the back stack is possibly unecessary. If you simply have a menu list with each entry invoking a different fragment in a right pane then you might get away with calling 'add' on each (don't call addToBackStack) and simply hide/show as required. Otherwise hold a reference to each fragment, call replace and don't add to the back stack. – PJL Jun 06 '11 at 12:46
  • yeah, keeping a reference would save me the searching part... I will keep the tags atm anyway and see if I might need them somehow. If not I'll follow your suggestion. Do you mind updating your answer so the information is easier to find when it is the accepted answer? – jellyfish Jun 06 '11 at 13:01
  • 5
    @jellyfish, be mindful of [link](http://stackoverflow.com/questions/6250580/fragment-already-added-illegalstateexception) as there is a bug which means that you shouldn't use `replace` if replacing a fragment that was previously popped. Use `remove()` and `add()` instead. – PJL Jun 08 '11 at 09:13
  • uh... thank you ever so much. Good I didn't change it until now. :) – jellyfish Jun 08 '11 at 09:14
4

To avoid the

IllegalStateException: Fragment already added

I found a workaround that is working fine for me: use remove(AFrag) and add(BFrag) in your transaction, instead of replace().

It looks like it is a bug: 4th comment in the accepted answer.

Community
  • 1
  • 1
Axel M. Garcia
  • 5,138
  • 9
  • 27
  • 29
  • PJL has mentioned something similar in his comment. So remove/add keeps the Fragment alive and its content saved? – jellyfish Jun 08 '11 at 14:47
  • yes, I keep a reference to mAFrag and to mBFrag, after the remove/add operation and the popBackStack mAFrag keeps his content. Furthermore, if I go forward again, mBFrag also keeps the content. I have just discovered @PJL comment, it was hidden. Sorry :S – Axel M. Garcia Jun 09 '11 at 08:20
  • but remove() also deletes the parent view of the fragment while replace() does not – Peter Ajtai Nov 27 '11 at 07:58
1
fragmentTransactionOnClick.setTransition(FragmentTransaction.TRANSIT_EXIT_MASK);

if you add the .setTransition.exit transit_exit_mask then the previous views wont come

Botz3000
  • 39,020
  • 8
  • 103
  • 127
anil
  • 11
  • 1
0

I found a way using the "tag" function:

//...
fragmentTransaction.add(R.id.framelayout_content, fragment1, "foo");
fragmentTransaction.add(R.id.framelayout_content, fragment2, "bar");

//...

fragmentManager.findFragmentByTag("foo");
fragmentManager.findFragmentByTag("bar");

However, this seems to work a bit asynchronously. Calling findFragmentByTag straight after the commit would return null. Only later, in my case in an OnOptionsItemSelected event, the fragments were found.

There is also a function called findFragmentById(int), but it's not very useful. The Id - if not specified in an XML layout - is the same as the container's, so in this case R.id.framelayout_content. If you later call the function with this ID, you can only access one of the attached Fragments. (I guess it's the last one, but haven't checked. It seems to be always the same one, though.)

I didn't by a quick glimpse find a way to get the "active" Fragment from my FrameLayout, so I think I'm going to save the tag of the last shown Fragment somewhere.

jellyfish
  • 7,868
  • 11
  • 37
  • 49
  • 1
    "Calling findFragmentByTag straight after the commit would return null" might be caused by: > Calling commit() does not perform the transaction immediately. Rather, it schedules it to run on the activity's UI thread (the "main" thread) as soon as the thread is able to do so." [Android Developers] http://developer.android.com/guide/components/fragments.html#Transactions) – PKeno Aug 03 '12 at 09:09