android onSaveInstanceState应用实例

//activity销毁之前调用,把状态值存储上

@Override
protected void onSaveInstanceState(Bundle outState) {
  outState.putBoolean("isConflict", false);
  outState.putBoolean(Constant.ACCOUNT_REMOVED, false);
  super.onSaveInstanceState(outState);
}

然后再oncreate里面判断

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//如果activity销毁之前保存了。savedInstanceState 就不是null
if (savedInstanceState != null && savedInstanceState.getBoolean(Constant.ACCOUNT_REMOVED, false)) {
// 防止被移除后,没点确定按钮然后按了home键,长期在后台又进app导致的crash
// 三个fragment里加的判断同理
DemoHelper.getInstance().logout(true,null);
finish();
startActivity(new Intent(this, LoginActivity.class));
return;
} else if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false)) {
// 防止被T后,没点确定按钮然后按了home键,长期在后台又进app导致的crash
// 三个fragment里加的判断同理
finish();
startActivity(new Intent(this, LoginActivity.class));
return;
}

原文地址:https://www.cnblogs.com/zhaoleigege/p/5064585.html