FragmentStatePageAdapter does this all for you. You shouldn't be keeping a separate map or whatever of fragment objects. If there is state you want to retain across instances, you need to implement onSaveInstanceState() to save it. This will be called by FragmentStatePageAdapter for you when it destroys the existing instance, and that state is supplied to the new instance when it is created.
On Mon, Aug 1, 2011 at 4:19 PM, Android Noob <[email protected]> wrote: > I'm relatively new to android and I'm having trouble trying to save > the state of Fragments as I switch from one page to the next using the > FragmentStatePageAdapter. > > I have a ViewPager in an activity with five fragments in it and a > HashMap<Integer, Fragment> that's used to cache fragments. In > fragment 0, I have a login screen that displays an Avatar and the user > name when the user is successfully logged in. When I flip to fragment > 2, I see that fragment 0 gets destroyed (and fragment 3 gets > instantiated). When I flip back to fragment 1, fragment 0 gets > loaded, but a new instance of it gets created instead of restoring the > previous state so my login screen is back to the state it was before > the user successfully logged in. > > My question is, how do i switch back and forth with this caching > scheme while still maintaining fragment state? I just can't seem to > get it right. > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- Dianne Hackborn Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

