Activity的onSaveinstaceState()保存fragment状态

Activity的onCreat方法:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();if (savedInstanceState != null) {
            homeFragment = (HomeFragment) supportFragmentManager.getFragment(savedInstanceState,"homeFragment");   
        }
        super.onCreate(savedInstanceState);
    }

重写onSaveinstaceState()方法:

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (homeFragment!=null){
            getSupportFragmentManager().putFragment(outState,"homeFragment",homeFragment);
        }
    }

 

posted on 2018-09-25 16:07  LoaderMan  阅读(530)  评论(0编辑  收藏  举报

导航