Fragments in are not restored after orientation change - android

In my program, I have the following hierarchy:
Activity
Fragment
ViewPager + FragmentStatePagerAdapter
Fragment containing video
The fragment that is nested immediately in the activity is initialized using setRetainInstance(true). This led to a crash whenever switching orientation. Used the solution described in the bug report: https://code.google.com/p/android/issues/detail?id=42601#c10.
Still, the app would crash whenever I would switch orientation. I found another bug report + solution: https://code.google.com/p/android/issues/detail?id=42601#c32.
After applying this solution I was able to rotate the device without the app crashing. However, the deeply nested fragment did not retain its state. As this fragment contains a video that should continue playing in spite of any orientation change, this is a must-have. I found a third bug report here describing this is a known issue in android support library versions 20 an up: https://code.google.com/p/android/issues/detail?id=74222#c17
Now, it does seem that the deepest fragment is retaining some kind of state. At least it is not destroyed, since the audio of the video keeps playing throughout and after the orientation change. However, the fragment is not restored after the orientation change. I simply see a white rectangle where the video should be.
As you can see, this is getting ridiculous. I have already had to use three hacky solutions to bugs in the ViewPager class, and it is still not working properly. If anybody has any idea what else I can try, I will be very much obliged.

Eventually, I did not find a solution to the problem. I have solved the problem by retaining the state of the nested fragments in the parent fragment, which does correctly retain state. To do this, I did the following:
Add a HashMap to the parent fragment
Gave every fragment a unique ID string that could be restored whenever the fragment was recreated
Created a state object for all nested fragments in their onCreate method and added it to the HashMap in the parent fragment. Or, if ((ParentFragment)getParentFragment()).stateMap.contains(id), restore the existing state.
Moved all member variables of the nested fragment to the StateObjects

Related

Retaining Fragment instance (no configuration change) with ViewPager

As the title suggests, I want to retain my fragments. I have enforced my app orientation to always be in landscape mode in the manifest file so that there will be no rotations.
I have read:
Understanding Fragment's setRetainInstance(boolean)
and
Why use Fragment#setRetainInstance(boolean)?
However, I am not sure if they apply to my situation.
My project consists of a ViewPager with swipe tabs. How can I ensure that the fragments used in the ViewPager are the same ones as created when the MainActivity first starts? Would I use the same tagging method and findfragment by tag?
Additionally, where would it be best to check for the tagged fragment, else create a new fragment?
Just a side question related to ViewPager: what can I do to immediately create all fragments used in the ViewPager when the mainactivity is started, rather than to wait for a swipe event to occur?
To answer your last question view pager will by default create the fragments around the current fragment so you don't need to worry about that part. What I would recommend is let the view pager manage your fragments for you rather changing the behaviour since you might face performance issues.
SetRetainInstance simply keeps the instance of your fragment when its detached so it's up to you to assess whether you need to use it or not.. is there anything you want to maintain about that fragment? if not then do not use it, free your memory as much as possible.
For the last question, why do you want the same fragments created from the first time to be retained ? all the time? if you NEED to do that then rethink your structuring. gracefully recreate your fragments and maybe have some caching of your data on another layer if that is what you are worried about.

Retaining Fragment backstack on orientation change

CONSTRUCTION
I've got Activity that holds a "path" made of Fragments.
The User goes from Fragment1 to Fragment2 and then to Fragment3 filling up the required informations. Nothing fancy here just plain backStack and .replace made on Fragments.
QUESTION
How should I retain that information when orientation changes to get my backStack back the way it was before orientation change?
IDEAS
The only idea I have is to insert into saveInstanceState the last visible Fragment and recreate things from there but it feels really hacky and I think in the long run it's going to make some major problems.
/////////////UPDATE//////////////
It looks like nowadays Android is capable of doing it on its own as long as you are following the guidelines provided by ARTICLE
The backstack itself is saved and loaded automatically with the activity.
Here's an article how Android does save and load activity and fragment states.
What you should care of is implementing the same logic of saving and restoring states for each fragment.

Advantage of fragments on Screen rotation

I wonder if there is any advantage of fragments, on screen rotation.
Generally fragments get destroyed followed by activity. Is there something that fragments retain while doing so?
onDestroy() method is called both in the activity and fragments.
I can try to figure out advantage of Fragment on Screen rotation.
Realtime app problem is:
Android is the potentially frequent destruction and reconstruction of an Activity. The most common time this occurs is when the user rotates the device between horizontal and portrait orientations (Screen rotation).
This crashing usually occurs because device orientation changes cause the Android framework to tear down the displayed Activity along within any contained Views, and then to fully reconstruct the Activity/View hierarchy. Any references to the Activity or to the Views within the Activity suddenly become invalid. Similarly any references within the Activity or Views that were set as a result of a user action or similar are now lost.
There are a number of ways to deal with this issue but one of the easiest is to take advantage of Fragments.
Things to keep in mind:
Fragments won’t automatically resolve this issue because, by default, when the Activity is torn-down in response to an orientation change the Fragment contained within the Activity is also torn down along with any contained Views.
The solution lies in an underused method: Fragment.setRetainInstance with a value of true.
why?
Calling setRetainInstance with a value of true causes Android to preserve the Fragment across the teardown/reconstruction cycle of an Activity. Along with the Fragment, the Views or other object references contained within the Fragment or Views remain.
With setRetainInstance(true) called on a Fragment instance.when an orientation change occurs, Android…
Holds a reference to the Fragment instance
Tears down the old Activity instance
Creates a new Activity instance
Attaches the preserved Fragment instance to the new Activity instance
you must add
android:configChanges="keyboardHidden|orientation|screenSize"
in parent Activity also calling
setRetainInstance(true)
in onCreate of fragment

Situation where you would use FragmentTransaction.replace instead or show/hide or detach/reattach?

After spending a fair bit of time figuring out that the reason my fragments chosen from a drawer layout weren`t displaying sometimes due to the choreographer skipping frames (I was using transaction.replace rather than show/hide) it made me wonder -- what are the situations where one would want to use replace rather than show/hide or detach/reattach? My problem went away when I switched to using show/hide btw.
Taken from this thread I got this on what happens when you call FragmentTransaction.replace():
Android will effectively perform a sequence of
FragmentTransaction.remove(...) (for all Fragments currently added to
that container) and FragmentTransaction.add(...) (for your supplied
Fragment). Removing a Fragment from the FragmentManager will cause the
Fragment to be destroyed and its state will no longer be managed. Most
noticeably, when you re-add the Fragment all of the views will have
been reset. Note: since you are reusing the same Fragment instance,
the Fragment will still keep the value any instance variables.
and from this thread I got that it is probably better to show/hide rather than replace if you plan on using that fragment again. My question is, in which situations do you use FragmentTransaction.Replace()? The only place I could see it really being useful is for something you know you won`t need again, kind of like a dialog picker with options but I use dialog fragments for those situations.
Does anyone use FragmentTransaction.replace regularly, and if so, why did you choose that over another method? Cheers
It maybe useful, for example, when implementing a deep fragments hierarchy in Multi-pane pattern (when click on item in the right fragment moves it to the position of the left).
Also, since hiding a Fragment keeps it in FragmentManager, it maybe expensive if you have a heavy content in it or hide multiple instances. Calling remove() or replace() and properly saving fragment's state is more Android-way, I think.

How to save Fragment state across orientation changes using FragmentStatePagerAdapter?

I'm using a ViewPager with a FragmentStatePagerAdapter and I'm having problems saving the Fragment's state across orientation changes. It works fine when paging back and forth e.g. viewing a page, swiping two pages away, and then going back 2 pages to the original Fragment correctly saves and restores state. I am doing this using onSaveInstanceState and restoring state in onCreateView if the Bundle isn't null.
Changing orientations, however, doesn't work through the same mechanism and from my testing doesn't even call the fragment's onSaveInstanceState method.
Is this expected? Am I missing something to force it to save instance state? Did I do something to stop it from working?
Thanks!
Turns out I wasn't calling super.onSaveInstanceState() in the Activity so none of the ViewPager/Fragment state was maintained. Adding in super.onSaveInstanceState() fixed it!

Categories

Resources