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); } }
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!