Android Fragment onDestroy在方向更改期间调用了两次

时间:2014-01-18 02:53:26

标签: android fragment

我不明白为什么当设备的方向发生变化时,为以下代码中的片段claass记录onDestroy两次。有人可以向我解释我做错了什么吗?

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");
    }


}

这里的片段是片段类

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");
    }
}

这是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

1 个答案:

答案 0 :(得分:6)

似乎使用支持库中的FragmentActivity会自动保存和恢复实例。因此,如果savedInstanceStatenull,则仅执行您的片段交易。

例如,在FragmentActivity' onCreate()中,执行以下操作:

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