-
Notifications
You must be signed in to change notification settings - Fork 0
Retained Fragment
Leonardo Miceli edited this page Jun 1, 2016
·
5 revisions
Retained Fragments are kept on rotation while the activity, fragment manager and views are destroyed. onCreate is not called, but onCreateView does.
Useful to avoid loading expensive data and to allow uninterrumpted execution (sound)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
mBeatBox = new BeatBox(getActivity());
}The retained state is only entered into when two conditions are met:
- setRetainInstance(true) has been called on the fragment.
- The hosting activity is being destroyed for a configuration change (typically rotation).
A fragment is only in the retained state for an extremely brief interval – the time between being detached from the old activity and being reattached to the new activity that is immediately created.
Retained fragments should be used only when clearly necessary. If your activity is destroyed because the OS needs to reclaim memory, then all your retained fragments are destroyed, too, which may mean that you lose some data.
by lmiceli