请问如何使用 onSavedInstanceState 示例

当涉及到拯救一个州的时候,我很困惑。所以我知道当活动即将被销毁时,就会调用 onSaveInstanceState(Bundle)。但是如何在 onCreate(Bundle savedInstanceState)中存储信息并将其恢复到原来的状态呢?我不明白这个捆绑包将如何恢复信息。如果有人能提供一个例子会很有帮助。 Dev 指南没有很好地解释这一点。

public class Conversation extends Activity {
private ProgressDialog progDialog;
int typeBar;
TextView text1;
EditText edit;
Button respond;
private String name;
private String textAtView;
private String savedName;


public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);


setContentView(R.layout.dorothydialog);
text1 = (TextView)findViewById(R.id.dialog);
edit = (EditText)findViewById(R.id.repsond);
respond = (Button)findViewById(R.id.button01);


if(savedInstanceState != null){
savedInstanceState.get(savedName);
text1.setText(savedName);
}
else{
text1.setText("Hello! What is your name?");
respond.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
name = edit.getText().toString();
text1.setText("Nice to meet you "+ name);
}
});
}
}


@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putString(savedName, name);
}
}
132348 次浏览

基本上 onSaveInstanceState (Bundle outBundle)会提供一个 Bundle。 When you look at the Bundle class, you will see that you can put lots of different stuff inside it. At the next call of onCreate(), you just get that Bundle back as an argument. 然后您可以再次读取您的值并恢复您的活动。

假设您有一个具有 EditText 的活动,用户在其中写入了一些文本。 之后,系统调用 onSaveInstanceState ()。 从 EditText 读取文本,并通过 Bundle.putString (“ edit _ text _ value”,theValue)将其写入 Bundle。

现在调用 onCreate。检查提供的包是否为 null, 您可以通过 Bundle.getString (“ edit _ text _ value”)恢复您的值,并将其放回 EditText 中。

Bundle是要保存的所有信息的容器。您可以使用 put * 函数向其中插入数据。这里有一个简短的列表(还有更多)的 put 函数,您可以使用它们在 Bundle中存储数据。

putString
putBoolean
putByte
putChar
putFloat
putLong
putShort
putParcelable (used for objects but they must implement Parcelable)

onCreate函数中,这个 Bundle被返回给程序。检查应用程序是重新加载还是第一次启动的最佳方法是:

if (savedInstanceState != null) {
// Then the application is being reloaded
}

要将数据返回,可以使用 get * 函数,就像 put * 函数一样。数据存储为名称-值对。这就像散列表。你提供一个键和值,然后当你想要返回值的时候,你给出键,函数得到值。这里有一个简短的例子。

@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("message", "This is my message to be reloaded");
super.onSaveInstanceState(outState);
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
String message = savedInstanceState.getString("message");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}

您保存的信息将烤到屏幕上。希望这有所帮助。

所有新的 Android 开发人员应该知道的一个主要注意事项是,只要你为 Widgets (TextView、 Buttons 等)分配了 ID,它们中的任何信息都会被 Android 自动持久化。因此,这意味着大多数 UI 状态可以毫无问题地得到处理。只有当您需要存储其他数据时,这才会成为一个问题。

来自 Android 文档:

你唯一需要的工作就是 提供一个唯一的 ID (使用 Id 属性) 你想保存它的状态。如果 小部件没有 ID,那么它 无法拯救它的国家

这是额外的信息。

想象一下这个场景

  1. 启动活动。
  2. 启动一个新的 ActivityAPrime

    Intent intent = new Intent(getApplicationContext(), ActivityA.class);
    startActivity(intent);
    
  3. ActivityAPrime has no relationship with ActivityA.
    In this case the Bundle in ActivityAPrime.onCreate() will be null.

If ActivityA and ActivityAPrime should be the same activity instead of different activities, ActivityB should call finish() than using startActivity().

一个很好的信息: 您不需要检查 Bundle 对象在 onCreate ()方法中是否为 null。使用 onRestoreInstanceState ()方法,系统在 onStart ()方法之后调用该方法。系统仅当存在要还原的保存状态时才调用 onRestoreInstanceState () ,因此不需要检查 Bundle 是否为 null

If Data Is not Loaded From savedInstanceState use following code.
问题是 url 调用没有完全完成,所以检查数据是否已加载,然后显示 instanceState 值。

//suppose data is not Loaded to savedInstanceState at 1st swipe
if (savedInstanceState == null && !mAlreadyLoaded){
mAlreadyLoaded = true;
GetStoryData();//Url Call
} else {
if (listArray != null) {  //Data Array From JsonArray(ListArray)
System.out.println("LocalData  " + listArray);
view.findViewById(R.id.progressBar).setVisibility(View.GONE);
}else{
GetStoryData();//Url Call
}
}

存储信息:

static final String PLAYER_SCORE = "playerScore";
static final String PLAYER_LEVEL = "playerLevel";


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(PLAYER_SCORE, mCurrentScore);
savedInstanceState.putInt(PLAYER_LEVEL, mCurrentLevel);


// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}

如果您不想恢复 onCreate-Method 中的信息:

下面是一些例子: Recreating an Activity

您可以选择实现 onRestoreInstanceState () ,而不是在 onCreate ()期间还原状态,系统在 onStart ()方法之后调用该状态。系统仅当存在要还原的保存状态时才调用 onRestoreInstanceState () ,因此不需要检查 Bundle 是否为 null

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


// Restore state members from saved instance
mCurrentScore = savedInstanceState.getInt(PLAYER_SCORE);
mCurrentLevel = savedInstanceState.getInt(PLAYER_LEVEL);
}