Solution to the problem of fragment overlap and masking in Android

1. Causes of fragment overlap and covering

Mainly because of the state saving mechanism of fragment, when the system memory is insufficient, the main activity of fragment is recycled, and the instance of fragment is not recycled.

When the activity is recycled by the system, it will actively call onsaveinstance() method to save the view hierarchy. Therefore, when the activity is rebuilt again through navigation, the previously instantiated fragments will still appear in the activity. However, it is obvious from the above code that a new fragment is rebuilt again, To sum up, these factors cause multiple fragments to overlap.

2. If this problem is not a necessary problem, you can turn on "do not keep activity" in the developer option during debugging, and the activity will be recycled in time in order to simulate the activity.

3. Onsaveinstancestate() and onrestoreinstancestate().

Onsaveinstancestate (), which was supposed to be called only when the system was due to memory recycling activities, was also called in the operations of jumping to other activities, opening multitasking windows, and using home to return to the main screen. However, onrestoreinstancestate () was not called when it returned to the activity again. Moreover, the fragment I found before onresume () was only removed, not empty, so even if you execute the fragment method created in question 1 in onresume (), it is also useless. So it fails through remove().

Then, through the survey data, it is found that there is a sentence super.onrestoreinstancestate (savedinstancestate) in onsaveinstancestate() in activity. Google's interpretation of this sentence is "always call the superclass so it can save the view hierarchy state", which roughly means "always execute this code to call the parent class to save the state of the view layer". In fact, you will understand that this sentence leads to ghosting, so I deleted this sentence, and then oncreate() and onrestoreinstancestate() use the create fragment method in question 1 at the same time, and then save the switching state. It is found that the result is very perfect.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>