Reusing Fragments in a Fragmentpageradapter

reusing fragments in a fragmentpageradapter

The FragmentPagerAdapter already caches the Fragments for you. Each fragment is assigned a tag, and then the FragmentPagerAdapter tries to call findFragmentByTag. It only calls getItem if the result from findFragmentByTag is null. So you shouldn't have to cache the fragments yourself.

FragmentPagerAdapter not restoring fragments upon Orientation Change

As it turns out, the RetainInstance was causing a conflict between itself and the ViewPager/Adapter/FragmentManager doing their things . Removing it caused the Pager to properly rebuilt the Fragment, including the TextView I had, where it did not before. I also start to recieve a Bundle in the OnCreateView, where that was always null before with the RetainInstance set to True.

I had to remove the RetainInstance and utilize the OnSaveInstanceState and OnCreateView to pass in the current status of the Fragment before it was destroyed, and then re-create it in OnCreateView to reset the Fragment to its state before it was destroyed.

I was hoping that the Runnable that I was using to do the countdown would survive, or I would be able to reattach it, but I couldn't find a way. I had to save the current count in Milliseconds, and pass back to the Fragment to continue where it left off. Its not that big of a deal, but I am curious to see if you can truely re-attach all those things. The Runnable DOES still continue after the config change, but it doesn't update anything on the UI anymore, so I try to cancel the callbacks and null it when I'm inside OnSaveInstanceState.

I'm also reading items where I only need to use RetainInstance for items that have a AsyncTask attached or another similar item....otherwise, just rebuild it within the code.



Related Topics



Leave a reply



Submit