gpt4 book ai didi

java - Android onRestoreInstanceState 没有完全更新方向变化

转载 作者:行者123 更新时间:2023-11-30 02:19:39 24 4
gpt4 key购买 nike

我在一个计数程序中遇到了一个问题,该程序显示了诸如 onCreate、onStart 等方法被调用了多少次,当我从 Activity1 转到 Activity2 然后再回到 Activity1 时它似乎工作正常但是当我切换时方向,只有 mResume 得到增加。因此,例如,当我启动应用程序并切换方向 3 次时,结果如下所示:

onCreate() calls:1  
onStart() calls:1
onResume() calls:4
onRestart() calls:1

另一个奇怪的事情是,当我从 onRestoreInstanceState 中注释掉 mResume、mRestart 和 mStart 时,突然有 2 个变量更新了 mStart 和 mResume。

public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);

Log.i(TAG, "Entered the onRestoreInstanceState() method");

// Restore state members from saved instance
//mResume = savedInstanceState.getInt(RESUME_KEY);
//mRestart = savedInstanceState.getInt(RESTART_KEY);
//mStart = savedInstanceState.getInt(START_KEY);
mCreate = savedInstanceState.getInt(CREATE_KEY);

这里是完整的代码

public class ActivityOne extends Activity {    
// Use these as keys when you're saving state between reconfigurations
private static final String RESTART_KEY = "restart";
private static final String RESUME_KEY = "resume";
private static final String START_KEY = "start";
private static final String CREATE_KEY = "create";

// String for LogCat documentation
private final static String TAG = "Lab-ActivityOne";

// Lifecycle counters

// TODO:
// Create variables named
// mCreate, mRestart, mStart and mResume
// to count calls to onCreate(), onRestart(), onStart() and
// onResume(). These variables should not be defined as static.

// You will need to increment these variables' values when their
// corresponding lifecycle methods get called.
int mCreate;
int mRestart;
int mStart;
int mResume;

// TODO: Create variables for each of the TextViews
// named mTvCreate, mTvRestart, mTvStart, mTvResume.
// for displaying the current count of each counter variable

TextView mTvCreate;
TextView mTvRestart;
TextView mTvResume;
TextView mTvStart;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);

// TODO: Assign the appropriate TextViews to the TextView variables
// Hint: Access the TextView by calling Activity's findViewById()
// textView1 = (TextView) findViewById(R.id.textView1);
mTvCreate = (TextView) findViewById(R.id.create);
mTvRestart = (TextView) findViewById(R.id.restart);
mTvStart = (TextView) findViewById(R.id.start);
mTvResume = (TextView) findViewById(R.id.resume);

Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo);
launchActivityTwoButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO:
// Launch Activity Two
// Hint: use Context's startActivity() method

// Create an intent stating which Activity you would like to
// start
Intent intent = new Intent(v.getContext(), ActivityTwo.class);

// Launch the Activity using the intent
startActivity(intent);

}
});

// Has previous state been saved?
if (savedInstanceState != null) {

// TODO:
// Restore value of counters from saved state
// Only need 4 lines of code, one for every count variable
mCreate = savedInstanceState.getInt(CREATE_KEY);
mRestart = savedInstanceState.getInt(RESTART_KEY);
mStart = savedInstanceState.getInt(START_KEY);
mResume = savedInstanceState.getInt(RESUME_KEY);

}

// Emit LogCat message
Log.i(TAG, "Entered the onCreate() method");

// TODO:
// Update the appropriate count variable
// Update the user interface via the displayCounts() method
mCreate += 1;
displayCounts();

}

// Lifecycle callback overrides

@Override
public void onStart() {
super.onStart();

// Emit LogCat message
Log.i(TAG, "Entered the onStart() method");

// TODO:
// Update the appropriate count variable
// Update the user interface
mStart += 1;
displayCounts();

}

@Override
public void onResume() {
super.onResume();

// Emit LogCat message
Log.i(TAG, "Entered the onResume() method");

// TODO:
// Update the appropriate count variable
// Update the user interface
mResume += 1;
displayCounts();

}

@Override
public void onPause() {
super.onPause();

// Emit LogCat message
Log.i(TAG, "Entered the onPause() method");
}

@Override
public void onStop() {
super.onStop();

// Emit LogCat message
Log.i(TAG, "Entered the onStop() method");
}

@Override
public void onRestart() {
super.onRestart();

// Emit LogCat message
Log.i(TAG, "Entered the onRestart() method");

// TODO:
// Update the appropriate count variable
// Update the user interface
mRestart += 1;
displayCounts();

}

@Override
public void onDestroy() {
super.onDestroy();

// Emit LogCat message
Log.i(TAG, "Entered the onDestroy() method");
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);

Log.i(TAG, "Entered the onSaveInstanceState() method");
// TODO:
// Save state information with a collection of key-value pairs
// 4 lines of code, one for every count variable
savedInstanceState.putInt(CREATE_KEY, mCreate);
savedInstanceState.putInt(RESTART_KEY, mRestart);
savedInstanceState.putInt(START_KEY, mStart);
savedInstanceState.putInt(RESUME_KEY, mResume);


}

public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);

Log.i(TAG, "Entered the onRestoreInstanceState() method");

// Restore state members from saved instance
mResume = savedInstanceState.getInt(RESUME_KEY);
mRestart = savedInstanceState.getInt(RESTART_KEY);
mStart = savedInstanceState.getInt(START_KEY);
mCreate = savedInstanceState.getInt(CREATE_KEY);

//displayCounts();
}

// Updates the displayed counters
// This method expects that the counters and TextView variables use the
// names
// specified above
public void displayCounts() {

mTvCreate.setText("onCreate() calls: " + mCreate);
mTvStart.setText("onStart() calls: " + mStart);
mTvResume.setText("onResume() calls: " + mResume);
mTvRestart.setText("onRestart() calls: " + mRestart);

}
}

这是xml

<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView android:id="@+id/create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/onCreate" />

<TextView android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/onStart" />

<TextView android:id="@+id/resume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/onResume" />

<TextView android:id="@+id/restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/onRestart" />

<Button android:id="@+id/bLaunchActivityTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button" />

</LinearLayout>

最佳答案

您将从 Bundle 中获取 mCreatemStartmRestartmResume 的值 对象两次,分别在 onCreateonRestoreInstanceState 中。这意味着在两次检索值之间对计数所做的任何更改都将被忽略。

如果它被调用,onRestoreInstanceState 会在 onCreate 之后被调用,所以你不应该恢复 onRestoreInstanceState 中的值,因为它们已经已在 onCreate 中恢复。

关于java - Android onRestoreInstanceState 没有完全更新方向变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28794885/

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