0

I tried to use putFragment to save reference for fragments for using it in future (and not recreate) before replace.

BaseFragment last = (BaseFragment) mContext.getSupportFragmentManager().getFragments().get(mContext.getSupportFragmentManager().getFragments().size() - 1);
mContext.getSupportFragmentManager().putFragment(mContext.getBundle(), last.getType().toString(), last);

And before creating new fragment i check bundle for fragment existing:

    BaseFragment f = (BaseFragment) getSupportFragmentManager().getFragment( mBundle, type.toString());

    if(f != null)
        return  f;
    else 
        // create new fragment

FragmentType is just my enum:

public static  enum FragmentType{
        PROJECTS, 
        BALANCE
}

But for all fragments (for all keys in bundle) it generates same integer value.

So getFragment method returns wrong fragment. Where is the problem?

I saw this post with same issue. But it still is not resolved...

I needed it for storing fragment state while replacing it. I tried it after this answer.

Community
  • 1
  • 1

2 Answers2

0

These methods (put, get) are meant to be used inside onSaveInatanceState and onRestoreInstanceState to save the "state" of the fragment so that it (the state) can be restored later.

Are you using them in that context?

If not, you are essentially trying to override the way the OS handles the fragments' lifecycle, which is asking for trouble.

In other words, don't try to get back the same instance, the system just cannot guarantee that to you.

There is an interesting discussion on this issue over at the android developers

joakim
  • 3,533
  • 2
  • 23
  • 28
  • It's also mentioned that you should not try to cheat the system by imposing a fragment recycling mechanism on top of what Android already does. As I said above, it's asking for trouble – joakim Feb 10 '15 at 16:09
  • so, how can i store fragment state while replacing it with another fragment? onSavedInstance doesn't work in this case – Suvitruf - Andrei Apanasik Feb 10 '15 at 16:31
0

Use cases of these methods are described in this question: get/putFragment()

Also, don't use getFragments(); It is a method annotated by @Hide and is not supposed to be used, because android can destroy an recreate activities and fragments at any given time, so the list is not always correct.

While the methods do give acces to the 'pointer' of the fragment, It is meant to only restore state.

Java is a language with a garbage collector, and android builds on top of that with objects that do not give us control over their lifecycle.

Community
  • 1
  • 1
ThMBc
  • 794
  • 13
  • 18
  • From the information you give me, I conclude that you want to hold a reference to the state of the fragment. The only state a fragment should have in android is your UI state, wich should be handled with onSaveInstance() and onRestoreInstance(), Possibly with some extras in that bundle for yourself. The reason why I would use get/putFragment() would be if I had 2 fragments of the same type, with different states and I should be able to differentiate between them (by the 'pointer' integer to the state. Having the 'pointer' does not give you acces to the object itself in Java. – ThMBc Feb 11 '15 at 07:37
  • onSaveInstance() and onRestoreInstance() isn't called if you replace fragment. – Suvitruf - Andrei Apanasik Feb 11 '15 at 07:58
  • The question you asked was in relation to get/putFragment(), wich is meant to be used inside your activity for when you restore state, so the correct fragment can be displayed inside your activity, since you would not know what fragment shuold be active if you restore it. (Note that this can also be used with FindFragmentByTag();) There is no other valid use-case for it. Perhaps you should formulate you question differently and ask about what you are trying to achieve. Which I think is how to save Fragment state? Pretty useful answer here: http://stackoverflow.com/a/22505654/4030105 – ThMBc Feb 11 '15 at 08:21
  • I need to save state of fragment (data that i got from web + some gui params) when I replace it using fragment manager. I tried to use get/putFragment() after advice in another thread. In your link after replacing fragment method onSaveInstanceState will not be called. – Suvitruf - Andrei Apanasik Feb 11 '15 at 09:12
  • indeed. It will not call onSaveInstanceState (which would only be called if there would be a configuration change), but the fragment will also not be destroyed with replace(). So if you want to recreate the view as it was and a avoid a network call check the answer I linked to in my previous comment. – ThMBc Feb 11 '15 at 09:53
  • It loss all state, When i open this fragment again, it will be recreate and lose, for example, position of ListView. – Suvitruf - Andrei Apanasik Feb 11 '15 at 09:58
  • Then you should hold it in a variable, and in onCreateView scroll to the position in the variable, if you initialize a variable with 0 then you don't even have to check wether the list was already there or not. Using fragments, you get a lot of freedom you did not have when using activities, but you have to implement more yourself. It doesn't lose state, but onCreateView() will get called again and your view is re-inflated, which seems like losing state, but the view is not the same as your state, of which you should be responsible – ThMBc Feb 11 '15 at 10:28