android - 如何使用保存实例状态保存 Activity 状态?

标签 android android-activity application-state

我一直在做Android SDK平台,对如何保存应用程序的状态有点不清楚。因此,考虑到“Hello, Android”示例的这个小的重新工具:

package com.android.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {

  private TextView mTextView = null;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mTextView = new TextView(this);

    if (savedInstanceState == null) {
       mTextView.setText("Welcome to HelloAndroid!");
    } else {
       mTextView.setText("Welcome back.");
    }

    setContentView(mTextView);
  }
}

我认为这对于最简单的情况就足够了,但无论我如何离开应用程序,它总是以第一条消息响应。

我确信解决方案就像覆盖 onPause 或类似的东西一样简单,但我已经在文档中查找了 30 分钟左右,但没有发现任何明显的东西。

最佳答案

您需要覆盖 onSaveInstanceState(Bundle savedInstanceState) 并将要更改的应用程序状态值写入 Bundle 参数,如下所示:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putBoolean("MyBoolean", true);
  savedInstanceState.putDouble("myDouble", 1.9);
  savedInstanceState.putInt("MyInt", 1);
  savedInstanceState.putString("MyString", "Welcome back to Android");
  // etc.
}

Bundle 本质上是一种存储 NVP(“名称-值对”)映射的方法,它将被传递到 onCreate()onRestoreInstanceState() 然后你可以从这样的 Activity 中提取值:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
  double myDouble = savedInstanceState.getDouble("myDouble");
  int myInt = savedInstanceState.getInt("MyInt");
  String myString = savedInstanceState.getString("MyString");
}

或者来自 fragment 。

@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    // Restore UI state from the savedInstanceState.
    // This bundle has also been passed to onCreate.
    boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
    double myDouble = savedInstanceState.getDouble("myDouble");
    int myInt = savedInstanceState.getInt("MyInt");
    String myString = savedInstanceState.getString("MyString");
}

您通常会使用这种技术来存储应用程序的实例值(选择、未保存的文本等)。

关于android - 如何使用保存实例状态保存 Activity 状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10831429/

相关文章:

Android 可绘制图像不显示

Android SearchView 通过 android 启动器图标?

android - onStart() 是确定 "user has re-opened the app"的最佳位置是否正确?

rest - 我对 Roy Fielding 的 HTTP cookie 的 REST 替代方案的解释是否正确?

java - 添加计时器并设置大小到 viewpager

java - 如何在 JodConverter 中读取 doc 或 pdf 中的文本

android - 检测 startActivityForResult 的调用者

java - 如何在 ScreenSlidePagerAdapter 中启动新 Activity ?

java - 如何上传图片到dropbox?