gpt4 book ai didi

android - 为什么 onStart 之后不调用 onRestoreInstanceState?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:57 26 4
gpt4 key购买 nike

我正在尝试测试 onRestoreInstanceState 方法以及何时(确切地)调用它。所以我遵循了这些步骤:

  1. 开始我的 Activity 。 onCreate --> onStart --> onResume 被调用。
  2. 按模拟器上的主页按钮。 onPause --> onSaveInstanceState --> onStop 被调用。
  3. 单击启动器中的图标并再次启动我的 Activity 。 onRestart --> onStart --> onResume 被调用。

我的java代码:

package com.test.demostate.app;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;


public class MainActivity extends ActionBarActivity {
private int visiters=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("TAG","onCreate");

}

@Override
protected void onPause() {
super.onPause();
Log.d("TAG","onPause");
}

@Override
protected void onStop() {
super.onStop();
Log.d("TAG","onStop");
}

@Override
protected void onStart() {
super.onStart();
Log.d("TAG","onStart");
}

@Override
protected void onRestart() {
super.onRestart();
Log.d("TAG","onRestart");

}

@Override
protected void onResume() {
super.onResume();
visiters++;
Log.d("TAG","onResume");
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("visiters",visiters);
Log.d("TAG",visiters+" visiters was saved ");
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
visiters=savedInstanceState.getInt("visiters");
Log.d("TAG",visiters+" visiters was restored");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d("TAG","onDestroy");
}
}

来自文档:您可以选择实现 onRestoreInstanceState(),而不是在 onCreate() 期间恢复状态,系统会在 onStart() 方法之后调用它。

所以调用onRestoreInstanceState

  • Activity 被销毁后 onPause --> onStop --> onDestroy 然后是 onCreate --> onRestoreInstanceState --> onResume(例如由于屏幕旋转)
  • Activity 停止后 onPause --> onStop --> onRestart --> onStart --> onRestoreInstanceState --> onResume(例如由于按下主页图标)

但是为什么不在 onStart 之后调用呢?

谢谢

最佳答案

仅当 onSaveInstanceState() 已被调用时,才在 onStart() 之后。

来自文档:

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).

This method is called between onStart() and onPostCreate(Bundle).

Activity#onRestoreInstanceState()

关于android - 为什么 onStart 之后不调用 onRestoreInstanceState?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24130726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com