我有一个包含三个页面的应用程序,其中一个是主页。如果用户转到两个子页面之一,用户可以输入一些我想保存的字段。我一直在研究 onPause() 和 onSaveInstanceState()。我想我只想对这两者有一个清晰的解释,以及 onPause() 是否更好以及代码示例。这就是我对 onSaveInstanceState() 的看法。
protected void onSaveInstanceState(Bundle outState) {
// Save away the original text, so we still have it if the activity
// needs to be killed while paused.
outState.putDouble("quizPts",qpts);
outState.putDouble("quizV",qvalue);
outState.putDouble("tPts",tpts);
outState.putDouble("tValue", tvalue);
outState.putDouble("hPts", hpts);
这就是我设置捆绑包的方式,通过给它一个ID和一个值。
public void onRestoreInstanceState(Bundle outState) {
super.onRestoreInstanceState(outState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
qpts = outState.getDouble("quizPts");
qvalue = outState.getDouble("quizV");
tpts = outState.getDouble("tPts");
tvalue = outState.getDouble("tValue");
hpts = outState.getDouble("hPts");
这就是我计划恢复它的方式,问题是我不明白如何传递 Bundle 来恢复它。我正在设置我需要返回到设置到 UI 的变量的变量。
任何建议都会很棒
感谢初学者androider