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
Android How to Implement Bottom Sheet from Material Design Docs
How to Make APK Secure. Protecting from Decompile
Why "This App Has Been Built with an Incorrect Configuration" Error Occured in Some Phones
Get Launchable Activity Name of Package from Adb
Handling Back Button in Android Navigation Component
Sqliteopenhelper Onupgrade() Confusion Android
App-Release-Unsigned.Apk Is Not Signed
Stuck with Getting Camera Pic When Using the Tab Activity
Value of a Global Variable Is Reset After It Is Initialised in Valueeventlistener
No Adapter Attached; Skipping Layout
Navigation Drawer to Switch Activities Instead of Fragments
Restrict Edittext to Single Line
Navigation Drawer with Backword Compatibility Android
Can't Get File Uri from Intent Onactivityresult
Send Touch Events to a Device via Adb