【发布时间】:2019-06-23 19:46:02
【问题描述】:
文档明确指出“This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState”。 StackOverflow 的几个问题concur。
但是我发现这并不完全准确,并且可能会产生误导。 onRestoreInstanceState 仅在水平-垂直之间切换方向时调用。每当调用 onStart 时都不会调用它。
我修改了这个simple lab exercise 以打印出日志跟踪,并将 mCount 保存到包中并显示它。
onRestoreInstanceState 在您按下返回按钮返回主 Activity 时不被调用也不在您按下 Home 按钮并重新启动时被调用。
即使我看到每次都调用了 onSaveInstanceState 和 onStop(从日志中),但在 onStart 之后我没有看到相应的 onRestoreInstanceState。这是出乎意料的。文档应该清楚地说明,只有当 onDestroy 被调用,然后在 onStart 之后,才会调用 onRestoreInstanceState。是否有其他情况(除了切换方向)可靠调用?
了解这些用例对于白盒测试很有用。
更新:android 文档已被澄清 - 请参阅下面的答案。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private int mCount = 0;
private TextView mShowCount;
public static final String EXTRA_MESSAGE =
"com.example.hellotoast.extra.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShowCount = (TextView) findViewById(R.id.show_count);
Log.d("MainActivity", "Hello World");
}
public void showToast(View view) {
Toast toast = Toast.makeText(this, R.string.toast_message,
Toast.LENGTH_SHORT);
toast.show();
Intent intent = new Intent(this, HelloCount.class);
intent.putExtra(EXTRA_MESSAGE, mCount);
startActivity(intent);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(EXTRA_MESSAGE, mCount);
super.onSaveInstanceState(outState);
Log.d("MainActivity", "onSaveInstanceState. saved mCount = " + mCount);
}
@Override
public void onRestoreInstanceState(Bundle b) {
super.onRestoreInstanceState(b);
Log.d("MainActivity", "onRestoreInstanceState");
if(b != null) {
mCount = b.getInt(EXTRA_MESSAGE);
Log.d("MainActivity", "onRestoreInstanceState mCount = "+mCount);
mShowCount.setText(Integer.toString(mCount));
}
}
@Override
protected void onStart() {
super.onStart();
Log.d("MainActivity", "onStart");
}
@Override
protected void onStop() {
super.onStop();
Log.d("MainActivity", "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("MainActivity", "onDestroy");
}
public void countUp(View view) {
mCount++;
if (mShowCount != null) {
mShowCount.setText(Integer.toString(mCount));
}
}
}
HelloCount.java
import static com.example.hellotoast.MainActivity.EXTRA_MESSAGE;
public class HelloCount extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("HelloCount", "onCreate");
setContentView(R.layout.activity_hello_count);
TextView tv = findViewById(R.id.helloCount);
int i = getIntent().getIntExtra(EXTRA_MESSAGE,0);
tv.setText(String.valueOf(i));
}
@Override
protected void onStart() {
super.onStart();
Log.d("HelloCount", "onStart");
}
@Override
protected void onStop() {
super.onStop();
Log.d("HelloCount", "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("HelloCount", "onDestroy");
}
}
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hellotoast.MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/button_toast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="@color/colorPrimary"
android:onClick="showToast"
android:text="@string/button_label_toast"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/show_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#FFFF00"
android:gravity="center_vertical"
android:text="@string/count_initial_value"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textSize="120sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_below="@+id/button_toast"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<Button
android:id="@+id/button_count"
android:layout_below="@+id/show_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@color/colorPrimary"
android:onClick="countUp"
android:text="@string/button_label_count"
android:textColor="@android:color/white" />
</RelativeLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="156dp"
android:layout_marginEnd="8dp"
android:text="Hello"
android:textColor="#8BC34A"
android:textColorHighlight="#00882A2A"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/helloCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.099" />
</android.support.constraint.ConstraintLayout>
【问题讨论】:
-
嗯...你的问题是什么?您是正确的,
onRestoreInstanceState()是作为配置更改的一部分调用的,而不是针对每个onStart()调用。 -
@CommonsWare 上面引用的文档,不要这么说:“
is called as part of a configuration change, not for every onStart() invocation”。这是误导。这也是为什么这么多人问 SO,为什么在 onStart 之后没有调用它 -
但是,这不是问题。 Stack Overflow 用于提问。如果您的目标是在 Stack Overflow 上提供与该主题相关的另一个答案,请回答您链接到的问题,或者将您上面写的内容转换为某个问题的答案。
-
“我正试图引起人们的注意”——然后提出问题并answer your own question。 “还想知道在调用它时是否还有其他情况(除了切换方向)”——然后针对该特定问题提出问题。
-
@CommonsWare Android 团队更新了文档以澄清:
This method is called between onStart() and onPostCreate(Bundle). This method is called only when recreating an activity; the method isn't invoked if onStart() is called for any other reason.developer.android.com/reference/android/app/…
标签: android