简体   繁体   中英

Android Fragment onDestroy called twice during a orientation change

I don't understand why onDestroy is being logged twice for the fragment claass in the the following code when the orientation of the device changes. Can someone explain to me what I am 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 here is 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 output 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

It seems that using FragmentActivity from the support library saves and restores instance automatically. Therefore, only do your fragment transactions if savedInstanceState is null .

For example, in your FragmentActivity 's onCreate() , do the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM