Android - Honeycomb - Action Bar tab fragments save/restore state
In the docs it says to ensure you save a 开发者_StackOverflow社区fragment's state when using tabs with the action bar so that when you switch tabs the fragment will look as it did before. How is this done? Using the sample for handling tabs, onSavedInstanceState is not called when the tab is switched so where would I save the state? Do I do it at the activity level? I only want the state to be transient.
Thanks
Fragment.onSaveInstanceState() is the proper place. That will be called when the state needs to be saved. If it isn't called, it doesn't need to be saved yet.
I ran into the exact same problem and came up with a solution that feels redundant (I assume this functionality will work better down the line), but it does work. The solution is to manage state bundles manually via the activity.
Saving state: In each fragment, implement a function called something like getState() that will return a bundle containing whatever state data the fragment wants to save. The activity should then call that before removing the fragment from the view.
Restoring state: Implement a method or constructor in the fragment that accepts the same bundle it saved before, and unpack the bundle to be used however it makes sense for the fragment. When the activity adds the fragment back to the view, pass the bundle in that the fragment earlier provided, and you should be set.
精彩评论