Android Fragment onDestroy causes orientation changes twice

I do not understand why onDestroy is registered twice for the claass fragment in the following code when the device orientation changes. Can someone explain to me what I'm doing wrong?

public class ExampleActivity extends Activity {

    protected String LOG_TAG = ExampleActivity.class.getSimpleName();

    private FrameLayout mFragmentHolder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LogUtil.i(LOG_TAG, "onCreate");

        setContentView(R.layout.activity_main);

        mFragmentHolder = (FrameLayout) findViewById(R.id.root);

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(mFragmentHolder.getId(),MyFragment.newInstance());
        ft.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtil.i(LOG_TAG, "onDestroy");
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtil.i(LOG_TAG, "onPause");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        LogUtil.i(LOG_TAG, "onRestoreInstanceState");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtil.i(LOG_TAG, "onResume");
    }

    @Override
    protected void onStart() {
        super.onStart();
        LogUtil.i(LOG_TAG, "onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtil.i(LOG_TAG, "onStop");
    }


}

And so the fragment is the fragment class

public class MyFragment extends Fragment {

    protected String LOG_TAG = MyFragment.class.getSimpleName();

    public static Fragment newInstance(){
        return new MyFragment();
    }

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

        View view = inflater.inflate(R.layout.screen_login, container,
                false);

        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.i(LOG_TAG, "onDestroy");
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtil.i(LOG_TAG, "onPause");
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.i(LOG_TAG, "onResume");
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtil.i(LOG_TAG, "onStart");
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtil.i(LOG_TAG, "onStop");
    }
}

And here is the result from logcat

01-17 22:04:34.661: I/BaseApplication(21513): [0.0.7]-[BaseApplication]-[main]-[01/17/2014 22:04:34] onConfigurationChanged
01-17 22:04:34.706: I/MyFragment(21513): [0.0.7]-[MyFragment]-[main]-[01/17/2014 22:04:34] onPause
01-17 22:04:34.711: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onPause
01-17 22:04:34.721: I/MyFragment(21513): [0.0.7]-[MyFragment]-[main]-[01/17/2014 22:04:34] onStop
01-17 22:04:34.726: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onStop
01-17 22:04:34.731: I/MyFragment(21513): [0.0.7]-[MyFragment]-[main]-[01/17/2014 22:04:34] onDestroy
01-17 22:04:34.736: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onDestroy
01-17 22:04:34.766: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onCreate
01-17 22:04:34.866: I/MyFragment(21513): [0.0.7]-[MyFragment]-[main]-[01/17/2014 22:04:34] onDestroy
01-17 22:04:34.876: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onStart
01-17 22:04:34.881: I/MyFragment(21513): [0.0.7]-[MyFragment]-[main]-[01/17/2014 22:04:34] onStart
01-17 22:04:34.886: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onRestoreInstanceState
01-17 22:04:34.891: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onResume
01-17 22:04:34.896: I/MyFragment(21513): [0.0.7]-[MyFragment]-[main]-[01/17/2014 22:04:34] onResume
+4
source share
1 answer

It seems that using FragmentActivityfrom the support library saves and restores the instance automatically. Therefore, perform fragment operations if savedInstanceState- null.

For example, FragmentActivity onCreate()follow these steps:

if(savedInstanceState == null){
   FragmentManager fragmentManager = getSupportFragmentManager();
   fragmentManager.beginTransaction()
   .replace(R.id.fragment_container, mFragment).commit(); //mFragment is your own defined fragment
}
+6
source

All Articles