The solution of multi-layer nested ghosting problem in Android fragment _android

Source: Internet
Author: User

1 The idea of solving bugs:

Step1: When bugs are found (excluding extremely low contingency, single sex, development tools cause)

Step2: Based on experience to judge the recurrence of a bug, multiple tests, until the precise positioning of the bug

Step3: Find the corresponding code based on the recurrence scenario

STEP4: Analyze whether the zone code will affect other functions.

STEP5: Do a good job of data backup. (Be prepared for code refactoring and recovery so you can be reckless with code)

STEP6: In the process of fixing the code, you'll find that there are a number of possible solutions. Try to take a solution that does not affect the main line. To avoid affecting other code.

STEP7: Review all reviews and test work. Think about the cause of the bug and avoid making the same type of mistake again.

2 cause reasons

Step1: In development, we often need to switch between multiple fragment and save the state of each fragment.

Step2: The official approach is to replace the fragment with replace (), but the call to replace () will cause fragment Oncreteview () to be invoked, so the current state cannot be saved when the interface is switched.

Step3: Therefore, the general use of Add (), hide () and show (), to achieve the state of saving fragment.

3 Principle Analysis

Step1: It is because of the use of fragment state preservation, when the system is out of memory, fragment host activity recycling, the fragment instance has not been recycled.

When Step2:activity is reclaimed by the system, the Onsaveinstance () method is actively invoked to hold the view layer (views hierarchy) and then the Onrestoreinstancestate () method is used to restore the view layer. (The core code for the two methods in the activity is as follows)
Step3: So when the activity is rebuilt again through navigation, the previously instantiated fragment will still appear in the activity
protected void onrestoreinstancestate (Bundle Savedinstancestate) {
if (Mwindow!= null) {
//Remove view layer
Bundle WindowState = Savedinstancestate.getbundle ( Window_hierarchy_tag);
if (WindowState!= null) {
mwindow.restorehierarchystate (windowstate);
}}} protected void Onsaveinstancestate (Bundle outstate) {
//Save View Layer
Outstate.putbundle (Window_hierarchy_tag, Mwindow.savehierarchystate ());
Parcelable p = mfragments.saveallstate ();
if (P!= null) {
outstate.putparcelable (Fragments_tag, p);
}
Getapplication (). Dispatchactivitysaveinstancestate (this, outstate);
}

4 Solutions (easy to understand after the solution is clear)

Solution1:

First, the simple way is to add a background color to each layer of fragment, and then you can't see it when you overlap.

Solution2:

Second, the operation of the null.

If empty, create object, add up;

If not empty, direct show out;

(Note Do not use the Remove method to remove, hide can)

The above is a small set to introduce the Android fragment multilayer nested ghosting problem solution, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.