SoFunction
Updated on 2025-04-12

Solution to the multi-layer nested ghosting problem of Android Fragment

1. Ideas for solving bugs:

//step1: When the bug is discovered (excluding extremely low chance, single-time, development tool causes)

//step2: Judging the scene of the bug based on experience, test it multiple times until the bug is accurately positioned

//step3: Find the corresponding code based on the scene

//step4: Analyze whether the area code will affect other functions.

//step5: Do a good job of data backup. (Prepare for code reconstruction and recovery, so that you can tinker with code unscrupulously)

//step6: During the process of repairing the code, you will find that there may be multiple solutions. Try to adopt solutions that do not affect the main line to avoid affecting other codes.

//step7: Review all reviews and testing and other work. Think about the reasons for the occurrence of bugs and avoid making the same type of mistakes next time.

2 Causes

//step1: During development, we need to frequently switch between multiple Fragments and save the status of each Fragment.

//step2: The official method is to use replace() to replace the Fragment, but the call to replace() will cause the Fragment's onCreteView() to be called, so the current state will not be saved when switching the interface.

//step3: Therefore, add(), hide() and show() are generally used to achieve the state of saving the Fragment.

3 Principle Analysis

//step1: It is precisely because the state saving of Fragment is used, when the system memory is insufficient and the host Activity of the Fragment is recycled, the instance of the Fragment is not recycled.

//step2: When the Activity is recycled by the system, it will actively call the onSaveInstance() method to save the view layer (View Hierarchy), and then restore the view layer through the onRestoreInstanceState() method. (The core code of 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 Activityprotected void onRestoreInstanceState(Bundle savedInstanceState) {
if (mWindow != null) {
//Take out the view layerBundle windowState = (WINDOW_HIERARCHY_TAG);
if (windowState != null) {
(windowState);
}
}
}
protected void onSaveInstanceState(Bundle outState) {
//Save the view layer(WINDOW_HIERARCHY_TAG, ());
Parcelable p = ();
if (p != null) {
(FRAGMENTS_TAG, p);
}
getApplication().dispatchActivitySaveInstanceState(this, outState);
}

4 Solutions (it will be easier to solve after understanding it clearly)

//solution1:

The first is to add background color to each layer of fragment, and then it will be invisible after overlapping.

//solution2:

Second. Perform empty-decision operation.

If empty, create an object and add it;

If it is not empty, show it directly;

(Be careful not to use the remove method to remove it, just hide)

The above is the solution to the multi-layer nested ghosting problem of Android Fragment introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!