Activity onDestroy 后不恢复数据
Activity not restoring data after onDestroy
我在使用两个 ActionBarActivities 时遇到了问题。
我有我的第一个 Activity (WorkoutEditActivity),它从 WorkoutEditActivity 中的一个片段开始另一个 Activity (A2) 并开始 ActivityForResult。
当我从 A2 按下 ActionBar 上的后退按钮时,我的第一个 Activity 被销毁,并将数据保存在 onSaveInstanceState 中,然后 Activity 重新启动,但我不知道如何恢复数据。
这是不起作用的相关代码:
public class WorkoutEditActivity extends ActionBarActivity{
int whatShouldBeSaved = -1;
Workout workout = null;
ViewPager viewpager;
ViewFactory factory = new ViewFactory(getSupportFragmentManager());;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("onCreate", "called");
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_edit_activity);
viewpager = (ViewPager) findViewById(R.id.viewPager);
viewpager.setOffscreenPageLimit(0);
viewpager.setAdapter(factory);
viewpager.setOnPageChangeListener(
new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
SettingsFragment.isActivated = true;
} else {
if (SettingsFragment.isActivated) {
SettingsFragment.isActivated = false;
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
);
if (savedInstanceState != null) {
whatShouldBeSaved = savedInstanceState.getInt("test", -1);
if (savedInstanceState.getSerializable("workout") != null){
setWorkout((Workout) savedInstanceState.getSerializable("workout"));
}
Log.d("GOT SAVED DATA", "" + whatShouldBeSaved);
}
Log.d("CREATE", "created? " + workout);
Log.d("CREATE", "created? " + whatShouldBeSaved);
}
public void setWorkout(Workout w){
Log.d("WORKOUT", "had workout " + workout);
workout = w;
factory.setWorkout(workout);
Log.d("WORKOUT", "has workout " + workout);
}
@Override
public void onResume() {
super.onResume();
Log.d("RESUME", "resuming act");
SettingsFragment.isActivated = true;
//factory.setViewPager(viewpager);
if (getIntent().getExtras() != null){
if (getIntent().getExtras().containsKey("workout")){
Log.d("RESUME", "resume has extra");
setWorkout((Workout) getIntent().getSerializableExtra("workout"));
whatShouldBeSaved = getIntent().getIntExtra("test", -1);
}
} else {
Log.d("RESUME", "extras did not contain workout2");
}
Log.d("RESUME", "created? " + workout);
Log.d("RESUME", "created? " + whatShouldBeSaved);
}
@Override
public void onPause(){
super.onPause();
Log.d("ONPAUSE", "called");
}
@Override
public void onStart(){
super.onStart();
Log.d("START", "starting act");
}
@Override
public void onStop(){
Log.d("STOP", "stopping act");
super.onStop();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Log.d("onACTRES", "called");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.d("SAVEINSTANCE", "called");
savedInstanceState.putInt("test", whatShouldBeSaved);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
Log.d("RESTOREINSTANCE", "called");
whatShouldBeSaved = savedInstanceState.getInt("test", 1);
Log.d("GOT SAVE", ""+ whatShouldBeSaved);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState){
super.onRestoreInstanceState(savedInstanceState, persistentState);
Log.d("RESTOREINSTANCE2", "called");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d("DESTROY", "onDestroy called");
Log.d("DESTROY", "destroying " + workout);
Log.d("DESTROY", "destroying " + whatShouldBeSaved);
Log.d("DESTROY", "was destroyed");
//first = true;
}
}
这是我日志的摘录
8151-8151/localhost.fitnessoptimizer D/STOP﹕ stopping act
01-10 13:26:45.110 8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ onDestroy called
01-10 13:26:45.110 8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ destroying Luffy
01-10 13:26:45.110 8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ destroying -1
01-10 13:26:45.110 8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ was destroyed
01-10 13:26:45.118 8151-8151/localhost.fitnessoptimizer D/onCreate﹕ called
01-10 13:26:45.118 8151-8151/localhost.fitnessoptimizer V/Monotype﹕ SetAppTypeFace- try to flip, app = localhost.fitnessoptimizer
01-10 13:26:45.118 8151-8151/localhost.fitnessoptimizer V/Monotype﹕ Typeface getFontPathFlipFont - systemFont = default#default
01-10 13:26:45.126 8151-8151/localhost.fitnessoptimizer W/ViewPager﹕ Requested offscreen page limit 0 too small; defaulting to 1
01-10 13:26:45.126 8151-8151/localhost.fitnessoptimizer D/CREATE﹕ created? null
01-10 13:26:45.126 8151-8151/localhost.fitnessoptimizer D/CREATE﹕ created? 1
01-10 13:26:45.127 8151-8151/localhost.fitnessoptimizer D/START﹕ starting act
01-10 13:26:45.129 8151-8151/localhost.fitnessoptimizer D/RESUME﹕ resuming act
01-10 13:26:45.129 8151-8151/localhost.fitnessoptimizer D/RESUME﹕ extras did not contain workout2
01-10 13:26:45.129 8151-8151/localhost.fitnessoptimizer D/RESUME﹕ created? null
01-10 13:26:45.129 8151-8151/localhost.fitnessoptimizer D/RESUME﹕ created? 1
01-10 13:26:45.152 8151-8151/localhost.fitnessoptimizer D/GOT WORKOUT﹕ null
onRestoreInstanceState 永远不会被调用,onCreates savedInstanceState 也不包含该信息。
如何保存或恢复锻炼(以及 whatShouldBeSaved,它实际上只是一个调试变量)?
PS: 我把我的代码放在 snippet js 标签里,因为它很长
来自官方网站onSaveInstanceState
Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).
当您在activity处按BACK时,它会关闭,但不会被杀死。 "Killing" 可以由 Android 系统完成,当应用程序处于后台并且系统需要临时释放一些资源时。
您需要改用 Shared Preferences。
onSaveInstanceState()只会在你的activity意外销毁时调用,当前activity销毁时不会调用。与 onRestoreInstanceState().
相同
官方网站,Recreating an Activity
我在使用两个 ActionBarActivities 时遇到了问题。 我有我的第一个 Activity (WorkoutEditActivity),它从 WorkoutEditActivity 中的一个片段开始另一个 Activity (A2) 并开始 ActivityForResult。 当我从 A2 按下 ActionBar 上的后退按钮时,我的第一个 Activity 被销毁,并将数据保存在 onSaveInstanceState 中,然后 Activity 重新启动,但我不知道如何恢复数据。 这是不起作用的相关代码:
public class WorkoutEditActivity extends ActionBarActivity{
int whatShouldBeSaved = -1;
Workout workout = null;
ViewPager viewpager;
ViewFactory factory = new ViewFactory(getSupportFragmentManager());;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("onCreate", "called");
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_edit_activity);
viewpager = (ViewPager) findViewById(R.id.viewPager);
viewpager.setOffscreenPageLimit(0);
viewpager.setAdapter(factory);
viewpager.setOnPageChangeListener(
new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
SettingsFragment.isActivated = true;
} else {
if (SettingsFragment.isActivated) {
SettingsFragment.isActivated = false;
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
);
if (savedInstanceState != null) {
whatShouldBeSaved = savedInstanceState.getInt("test", -1);
if (savedInstanceState.getSerializable("workout") != null){
setWorkout((Workout) savedInstanceState.getSerializable("workout"));
}
Log.d("GOT SAVED DATA", "" + whatShouldBeSaved);
}
Log.d("CREATE", "created? " + workout);
Log.d("CREATE", "created? " + whatShouldBeSaved);
}
public void setWorkout(Workout w){
Log.d("WORKOUT", "had workout " + workout);
workout = w;
factory.setWorkout(workout);
Log.d("WORKOUT", "has workout " + workout);
}
@Override
public void onResume() {
super.onResume();
Log.d("RESUME", "resuming act");
SettingsFragment.isActivated = true;
//factory.setViewPager(viewpager);
if (getIntent().getExtras() != null){
if (getIntent().getExtras().containsKey("workout")){
Log.d("RESUME", "resume has extra");
setWorkout((Workout) getIntent().getSerializableExtra("workout"));
whatShouldBeSaved = getIntent().getIntExtra("test", -1);
}
} else {
Log.d("RESUME", "extras did not contain workout2");
}
Log.d("RESUME", "created? " + workout);
Log.d("RESUME", "created? " + whatShouldBeSaved);
}
@Override
public void onPause(){
super.onPause();
Log.d("ONPAUSE", "called");
}
@Override
public void onStart(){
super.onStart();
Log.d("START", "starting act");
}
@Override
public void onStop(){
Log.d("STOP", "stopping act");
super.onStop();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Log.d("onACTRES", "called");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.d("SAVEINSTANCE", "called");
savedInstanceState.putInt("test", whatShouldBeSaved);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
Log.d("RESTOREINSTANCE", "called");
whatShouldBeSaved = savedInstanceState.getInt("test", 1);
Log.d("GOT SAVE", ""+ whatShouldBeSaved);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState){
super.onRestoreInstanceState(savedInstanceState, persistentState);
Log.d("RESTOREINSTANCE2", "called");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d("DESTROY", "onDestroy called");
Log.d("DESTROY", "destroying " + workout);
Log.d("DESTROY", "destroying " + whatShouldBeSaved);
Log.d("DESTROY", "was destroyed");
//first = true;
}
}
这是我日志的摘录
8151-8151/localhost.fitnessoptimizer D/STOP﹕ stopping act
01-10 13:26:45.110 8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ onDestroy called
01-10 13:26:45.110 8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ destroying Luffy
01-10 13:26:45.110 8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ destroying -1
01-10 13:26:45.110 8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ was destroyed
01-10 13:26:45.118 8151-8151/localhost.fitnessoptimizer D/onCreate﹕ called
01-10 13:26:45.118 8151-8151/localhost.fitnessoptimizer V/Monotype﹕ SetAppTypeFace- try to flip, app = localhost.fitnessoptimizer
01-10 13:26:45.118 8151-8151/localhost.fitnessoptimizer V/Monotype﹕ Typeface getFontPathFlipFont - systemFont = default#default
01-10 13:26:45.126 8151-8151/localhost.fitnessoptimizer W/ViewPager﹕ Requested offscreen page limit 0 too small; defaulting to 1
01-10 13:26:45.126 8151-8151/localhost.fitnessoptimizer D/CREATE﹕ created? null
01-10 13:26:45.126 8151-8151/localhost.fitnessoptimizer D/CREATE﹕ created? 1
01-10 13:26:45.127 8151-8151/localhost.fitnessoptimizer D/START﹕ starting act
01-10 13:26:45.129 8151-8151/localhost.fitnessoptimizer D/RESUME﹕ resuming act
01-10 13:26:45.129 8151-8151/localhost.fitnessoptimizer D/RESUME﹕ extras did not contain workout2
01-10 13:26:45.129 8151-8151/localhost.fitnessoptimizer D/RESUME﹕ created? null
01-10 13:26:45.129 8151-8151/localhost.fitnessoptimizer D/RESUME﹕ created? 1
01-10 13:26:45.152 8151-8151/localhost.fitnessoptimizer D/GOT WORKOUT﹕ null
onRestoreInstanceState 永远不会被调用,onCreates savedInstanceState 也不包含该信息。 如何保存或恢复锻炼(以及 whatShouldBeSaved,它实际上只是一个调试变量)?
PS: 我把我的代码放在 snippet js 标签里,因为它很长
来自官方网站onSaveInstanceState
Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).
当您在activity处按BACK时,它会关闭,但不会被杀死。 "Killing" 可以由 Android 系统完成,当应用程序处于后台并且系统需要临时释放一些资源时。
您需要改用 Shared Preferences。
onSaveInstanceState()只会在你的activity意外销毁时调用,当前activity销毁时不会调用。与 onRestoreInstanceState().
相同
官方网站,Recreating an Activity