简体   繁体   中英

setRetainInstance(true) in onCreate fragment in android

I am not holding any context in my fragment so I am using setRetainInstance(true) in UI fragment. can any one tell me whether this is the correct approach? Also where we should basically call setRetainInstance method? I mean in onCreate or onActivityCreated etc.

Introduction: setRetainInstance(boolean)

When you look at the Reference , you see:

  • onDestroy() will not be called (but onDetach() still will be, because the fragment is being detached from its current activity).
  • onCreate(Bundle) will not be called since the fragment is not being re-created.
  • onAttach(Activity) and onActivityCreated(Bundle) will still be called .

Then, where do I should use this method?

This method is used to retain a fragment when the configuration change. You need to set this method in fragment at its first creation . When you see above that onAttach or onActivityCreated will still be called after a rotation, don't set the method inside because you will called it again. It's useless...! The best approach is to call setRetainInstance inside onCreate method, because this last will not be called again after a changing rotation.

What's the correct approach to use it?

All depends what you want and do with your FragmentActivity and fragments. I'll answer with a quote by Alex Lockwood:

Retained fragments can be quite useful for propagating state information — especially thread management — across activity instances. For example, a fragment can serve as a host for an instance of Thread or AsyncTask , managing its operation. See my blog post on this topic for more information.

In general, I would treat it similarly to using onConfigurationChanged with an Activity ... don't use it as a bandaid just because you are too lazy to implement/handle an orientation change correctly. Only use it when you need to .

I'll try to put it as simply as possible.

Your fragment, probably, holds UI elements - like:

private TextView mView

These UI elements get initialized in onCreateView(LayoutInflater, ViewGroup, Bundle) method of a fragment, using the LayoutInflater class instance.

But this LayoutInflater instance uses Activity 's context to inflate your resources. So if your UI widgets are stored in your fragment class, they implicitly possess the Context of the associated activity - that is, the activity to which you've attached it to by commiting FragmentManager 's transaction.

Now imagine a configuration change occures (screen rotation, for example). Activity gets destroyed and a new one is created. Old Activity 's context normally should be garbage collected. But your retained fragment instance holds a strong reference to this context via each one of your stored UI elements, and thus this context is "reachable" in terms of GC, and should not be garbage-collected. A memory leak occurs.

That's why retained instance fragments should not be used as UI-fragments.

Just remember - even if you do not store a reference to Context object, like private Context context (that's what you asked), you might leak this context in a plenty of possibilities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM