Fragment overlap and masking

Source: Internet
Author: User

Fragment overlap and masking

1. Causes of overlap and coverage of Fragment

This is mainly because of the Fragment status saving mechanism. When the system memory is insufficient, the main Activity of Fragment is recycled, and the Fragment instance is not recycled.

When the Activity is recycled by the system, the onSaveInstance () method is called to save the View Hierarchy. Therefore, when the Activity is rebuilt through navigation, the previously instantiated Fragment will still appear in the Activity. However, it can be seen from the code above that the new Fragment is re-constructed, in summary, these factors cause multiple Fragment to overlap.

2. If this problem is not necessary, you can enable the "Do Not reserve Activity" option in the developer option during debugging, In order to simulate the Activity to be recycled in a timely manner.

3. onSaveInstanceState () and onRestoreInstanceState () methods.

The onSaveInstanceState () that was originally called only when the system recycled the Activity because of the memory is also called during the operations such as redirecting to another Activity, opening the multi-task window, and returning the Home to the Home screen, however, onRestoreInstanceState () is not called when it returns to Activity again. In addition, I found in onResume () that the previous Fragment was only removed and not empty. Therefore, even if you execute the Fragment method created in problem 1 in onResume (), it will not help. Therefore, a failure is declared through remove.

Then, we found that there is a super in onSaveInstanceState () in the Activity. onRestoreInstanceState (savedInstanceState), Google's explanation for this sentence is "Always call the superclass so it can save the view hierarchy state ", this code is always executed to call the parent class to save the view layer status ". As a matter of fact, we can see from here that this sentence has caused the appearance of a shadow, so I deleted this sentence, and then onCreate () and onRestoreInstanceState () use the Fragment method in question 1 at the same time, and then save the switchover status to find that the result is perfect.

// Record the location of Fragment private int position = 0; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_index); setTabSelection (position) ;}@ Override protected void onRestoreInstanceState (Bundle savedInstanceState) {position = savedInstanceState. getInt ("position"); setTabSelection (position); super. onRestoreInstanceState (savedInstanceState) ;}@ Override protected void onSaveInstanceState (Bundle outState) {// record the current position outState. putInt ("position", position );}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.