Will the Fragment be destroyed twice after the screen is rotated?

I load a fragment, in an Activity that the activity will be re-established when I rotate the screen, but shouldn"t the fragment in it be destroyed only once with activity? Why was it destroyed twice?

and why are people talking about screen rotation fragment overlap? When the activity is destroyed, isn"t the fragment associated with it destroyed as well? When you create a new activity, there is only one fragment,. How can it overlap?

the rookie said he was confused. I hope God can solve my question. thank you!

MainActivity

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager manager=getSupportFragmentManager();
        FragmentTransaction transaction=manager.beginTransaction();
        FragmentOne fragmentOne=new FragmentOne();
        transaction.replace(R.id.fragment_container,fragmentOne,"one");
        transaction.commit();

    }

FragmentOne

public class FragmentOne extends Fragment {

    private final String TAG=FragmentOne.class.getSimpleName();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG,"onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v=inflater.inflate(R.layout.fragment_one,container,false);

        Button button=v.findViewById(R.id.create_fragment);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager manager=getFragmentManager();
                FragmentTransaction transaction=manager.beginTransaction();
                transaction.replace(R.id.fragment_container,new FragmentTwo(),"two");
               // transaction.addToBackStack("one");
                transaction.commit();
            }
        });

        Log.i(TAG,"onCreateView");

        return v;

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        Log.i(TAG,"onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        Log.i(TAG,"onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();

        Log.i(TAG,"onDetach");
    }
}

Mar.28,2021

1. Why is there an overlap problem?
the onSaveInstanceState method (in FragmentActivity) is called when the screen is rotated and Activity is destroyed

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        markState(getSupportFragmentManager(), Lifecycle.State.CREATED);
        Parcelable p = mFragments.saveAllState();//
        if (p != null) {
            outState.putParcelable(FRAGMENTS_TAG, p);
        }
        ....
        
    }

mFragments.saveAllState () will eventually input the Fragment information managed in the current FragmentManager into Parcelable and return it. For more information, please see FragmentManagerImpl.saveAllState ()

.

when Activity recovers

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mFragments.attachHost(null /*parent*/);

        super.onCreate(savedInstanceState);
        ...
        if (savedInstanceState != null) {
            Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
            mFragments.restoreAllState(p, nc != null ? nc.fragments : null);//
        }
        ...
}

the code (FragmentManagerImpl) that mFragments.restoreAllState finally calls

void restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig) {
    ...
 for (int i=0; i<fms.mActive.length; iPP) {
            FragmentState fs = fms.mActive[i];
            if (fs != null) {
                FragmentManagerNonConfig childNonConfig = null;
                if (childNonConfigs != null && i < childNonConfigs.size()) {
                    childNonConfig = childNonConfigs.get(i);
                }
                // Fragment
                Fragment f = fs.instantiate(mHost, mContainer, mParent, childNonConfig);
                if (DEBUG) Log.v(TAG, "restoreAllState: active -sharp" + i + ": " + f);
                //FragmrntManager 
                mActive.put(f.mIndex, f);
                // Now that the fragment is instantiated (or came from being
                // retained above), clear mInstance in case we end up re-restoring
                // from this FragmentState again.
                fs.mInstance = null;
            }
        }
    ...
}

you can see that the Fragment is re-instantiated and added to the FragmentManger, which is displayed later. At this point, how you execute the transaction to add Fragment again in the onCreate of Activity will cause the Fragment to be added twice. When the Activity is destroyed, the associated Fragment is not actually destroyed, but the information is saved and re-instantiated when the Activity is restored.

2. The activity will be re-established when I rotate the screen, but shouldn't the fragment in it be destroyed only once with activity? Why was it destroyed twice?
when the Activity is destroyed, the Fragment is also destroyed for the first time.
execute replace in onCreate when Activity is restored. Note that there is already a Fragment in FragmentManager before replace, and the saved Fragment is restored when it is destroyed. Execute replace, and the saved Fragment is replaced (execute remove), and destroy, this is the second time.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb16fa-234c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb16fa-234c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?