关于开启不保留活动后引发RestoreInstanceState容易崩溃的问题小记

  • 由于在InstanceState方面的知识薄弱肤浅,故特意做下笔记,防止再犯下相同的错误。

当开启不保留活动后,APP会经常onStop,onDestroy,onTrimMemory,此时要可以容易的模拟调试App在后台内存不足的情况。

  • 在APP界面切换到其他APP界面(最方便的是进入系统设置)
onPause
onSaveInstanceState
onStop
onTrimMemory
onDestroy
  • 返回键返回到App时:
onCreate (has savedInstanceState)
//getIntent already had extras
onStart()
onRestoreInstanceState()
onResume()
...

此时,instanceState有数据

  • 如果这样写,界面的状态还在(EditText输入的内容还在,fragment的显示隐藏的状态也在),但是点击事件没反应了,即变量fragment也会是空的了。
public class TestActivity extends BaseActivity {
    private   TestFragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_my);
        if (savedInstanceState!=null) {
            return;
        }
        findViewById(R.id.imageView).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.i(TAG,"click:" + fragment );
                    }
        });
         fragment = TestFragment.newInstance("ttt");
    }
}
  • 如果要正确获取到Restore后的fragment,应该这样做
public class TestActivity extends BaseActivity {
    private   TestFragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_my);

        findViewById(R.id.imageView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG,"click:" + fragment);
            }
        });
        if (savedInstanceState!=null) {
            Log.i(TAG,"fragmentList:" + getSupportFragmentManager().getFragments());
            fragment = (TestFragment) getSupportFragmentManager().findFragmentByTag("fragment");
            Log.i(TAG,"fragment:" + fragment);
            return;
        }



        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        fragment = TestFragment.newInstance("ttt");
        ft.add(R.id.layout,fragment,"fragment");
        ft.commit();
  • 如果你不想用系统还原状态,太懒不想用findFragmentByTag把控件等东西找出来,就是要丢弃掉保存的状态,那就这样写:
public class TestActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(null);//just null
        setContentView(R.layout.fragment_my);
    }
}
  • 上面的Activity还原状态的例子中,Fragment的还原的流程是:
onCreateView(has savedInstanceState)
//getArguments already had extras
onViewCreated()

即被重新创建,并带有最原始给予的参数

  • 如果Fragment在FragmentPagerAdapter里面被销毁并RestoreInstanceState
onCreateView(has savedInstanceState)
//getArguments is new Bundle()
onViewCreated()

因为FragmentPagerAdapter在销毁时是不保存状态的,建议使用FragmentStatePagerAdapter

  • 如果MainActivity startActivityForResult到TestActivity,都销毁,返回后,MainActivity生命周期
onCreate
onStart
onRestoreInstanceState
onActivityResult
onResume

MainActivity重新创建,还原数据后拿到result继续处理

你可能感兴趣的:(android)