红糖糍粑
正如其他人和此处所述,Android 会自动为您处理。但是,如果你需要明确地添加任何东西去/来/从后台状态,那么你也可以重写时onSaveInstanceState()和onRestoreInstanceState()将被相应地调用方法。当您的 Activity 开始停止时,系统会调用该onSaveInstanceState()方法,以便您的 Activity 可以将状态信息保存到实例状态包中。此方法的默认实现保存有关 Activity 视图层次结构状态的瞬态信息,例如EditText小部件中的文本或小部件的滚动位置ListView。要为您的 Activity 保存其他实例状态信息,您必须覆盖onSaveInstanceState()并将键值对添加到在Bundle您的 Activity 意外销毁时保存的 对象。如果您覆盖onSaveInstanceState(),并且您希望默认实现保存视图层次结构的状态,则必须调用超类实现。@Overrideprotected void onRestoreInstanceState(Bundle outState) { if (outState != null) { Crashlytics.log(1, "FormActivity", "Method:onRestoreInstanceState, Msg: saved instance is not null"); if (outState.containsKey("record") && Session.getCurrentRecord() == null) { Session.setCurrentRecord( gson.fromJson( outState.getString("record"), Record.class ) ); } if (outState.containsKey("user") && Session.getCurrentUser() == null) { Session.setCurrentUser( gson.fromJson( outState.getString("user"), User.class ) ); } } super.onRestoreInstanceState(outState);}@Overrideprotected void onSaveInstanceState(Bundle outState) { Session.setCurrentRecord(record); outState.putString("record", gson.toJson(Session.getCurrentRecord())); outState.putString("user", gson.toJson(Session.getCurrentUser())); super.onSaveInstanceState(outState);}