I've got an activity, containing fragment 'list', which upon clicking on one of its items will replace itself to a 'content' fragment. When the user uses the back button, he's brought to the 'list' fragment again.
The problem is that the fragment is in its default state, no matter what I try to persist data.
Facts:
- both fragments are created through
public static TheFragment newInstance(Bundle args)
, setArguments(args)
and Bundle args = getArguments()
- both fragments are on the same level, which is directly inside a
FrameLayout
from the parent activity (that is, not nested fragments)
- I do not want to call
setRetainInstance
, because my activity is a master/detail flow, which has a 2 pane layout on larger screens. 7" tablets have 1 pane in portrait and 2 panes in landscape. If I retain the 'list' fragment instance, it will (I think) fuck things up with screen rotations
- when the users clicks an item in the 'list' fragment, the 'content' fragment is displayed through
FragmentTransaction#replace(int, Fragment, String)
, with the same ID but a different tag
- I did override
onSaveInstanceState(Bundle)
, but this is not always called by the framework, as per the doc: "There are many situations where a fragment may be mostly torn down (such as when placed on the back stack with no UI showing), but its state will not be saved until its owning activity actually needs to save its state."
- I'm using the support library
From the bullet 5 above, I guess that low-end devices that need to recover memory after a fragment transaction may call Fragment#onSaveInstanceState(Bundle)
. However, on my testing devices (Galaxy Nexus and Nexus 7), the framework doesn't call that method. So that's not a valid option.
So, how can I retain some fragment data? the bundle passed to Fragment#onCreate
, Fragment#onActivityCreated
, etc. is always null
.
Hence, I can't make a difference from a brand new fragment launch to a back stack restore.
Note: possible related/duplicate question
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…