Hi I have an activity that has two fragments.
Fragment B
Now the problem is if the user is in fragment B and config changes occur like screen rotation etc more than once then my that my ListView is empty because my arraylist is null.
Note that I am using onSavedInstanceState in fragment A and fragment B. If my current fragment is fragment A and config changes occur then there is no problem of restoring the state since in onCreateView I am getting the arraylist from bundle.
I know the reason why my arraylist is null when I comeback from fragment B to fragment A which is in backstack previously.When fragment A is in backstack the only method that is being called is onSaveInstanceState so after the first config change my arraylist field is null as I couldnt assign my arraylist stored in savedInstateState bundle to the arraylist field.
I do not want to use android:configchanges attribute in my manifest.
My Question is how can I restore the state of a fragment that is in backstack.
I achieved the goal of retaining the state of fragment which is in backstack by passing all the details of that fragment to main activity before it is added to backstack and saving the state in main activity's savedInstance state and whenever backstack changes and previous fragment is visible I am passing the saved instance state of that fragment to it from main activity and this way I am retaining the state. I am not sure if this the right approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With