I want to save the state of a fragment while switching fragments using androids navigation drawer. The fragment shouldn't refresh if it has been previously loaded. Is it possible?
To keep an fragment's state you have to call setRetainInstance(true)
inside the fragment's onCreate()
. What it does:
Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change).
That keeps the state across the activity recreation, but in this case your activity won't be recreated, you are manually switching fragments using the drawer. In that case, instead of creating a new fragment inside the drawer's click listener, you'd have to look for the fragment using either findFragmentById(int id)
or findFragmentByTag(String tag)
. If the value returned is null then you can safely create a new one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With