一、被其他任务打断(来电话),再次打开希望保留数据
private String SAVE_INSTANCE_TAG = "ATWAL"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cal); if (savedInstanceState != null) { String s = savedInstanceState.getString(SAVE_INSTANCE_TAG); } } //系统在回收当前Activity之前会调用,Back & Home 不会调用此方法 @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { if (outState != null) { outState.putString(SAVE_INSTANCE_TAG, "hello atwal"); } super.onSaveInstanceState(outState, outPersistentState); }
二、希望横竖屏切换、按了 Home or Back 键后再回来保留数据
@Override protected void onResume() { super.onResume(); String s = this.getPreferences(MODE_PRIVATE).getString(SAVE_INSTANCE_TAG, ""); } @Override protected void onPause() { super.onPause(); this.getPreferences(MODE_PRIVATE).edit().putString(SAVE_INSTANCE_TAG, "after pause"); }