These two days have been plagued by this problem, if the app run for a long time in the background, and then click into the crash will be, and fragment page overlap phenomenon, let me very unhappy. Study for a day, and finally understand the principles and solve them. The solution is as follows:
If the system does not have enough memory, or if you switch the screen, or the app runs in the background for a long time, the activity may be recycled by the system, and then fragment will not be recycled as the activity is recycled, causing the fragment to lose the corresponding activity.
Here, suppose we inherit from the Fragmentactivity class as mainactivity, where the fragment used is Fragmenta.
The changes in the app are: some reason the system recycle Mainactivity--fragmenta is not recycled--click App again--and first load the Fragmenta pages that are not recycled--because mainactivity is recycled, The system restarts the Mainactivity,fragmenta and it is reloaded again-the page is cluttered because a layer of fragmenta is covered above-(if Fragmenta uses the Getactivity () method), it will report a null pointer, Appear crash.
Although the principle is a bit round, the solution is simple:
Mainactivity overrides the Onsaveinstancestate method, Super.onsaveinstancestate (Outstate), and comments out so that it no longer saves the fragment state. To achieve its effect of being recycled together with mainactivity.