And now I feel quite confused. If Fragments are not destroyed, why those methods are executed by Android?
If you look at this diagram, Fragments
do not need to be destroyed to call onCreateView()
.
Do I have the guarantee that my Fragment object will be kept always with all fields (when user only moves beteween Fragments and doesn't leave the app)?
This depends on what you are doing. If your case is dealing with ViewPager
it really boils down to what PagerAdapter
you use and your ViewPager
configuration.
If not how should I save and restore its state? public void onSaveInstanceState(Bundle savedInstanceState) method seems to be only run when user left the app, not when user moves between Fragments.
It depends under what conditions you are wanting to restore state. For example, for orientation changes you can call setRetainInstance(true)
on the Fragment
and the Fragment's
state will be preserved when there is a configuration change given your Fragment
is not on the backstack.
Have you seen any good tutorial concerning ViewPager and Fragments?
The Android Guide has a good tutorial.