请问如何使用onSavedInstanceState示例

tj *_*ker 102 android savestate

归结为拯救一个州,我很困惑.所以我知道onSaveInstanceState(Bundle)当活动即将被销毁时会被调用.但是,如何将信息存储在其中并将其恢复到原始状态onCreate(Bundle savedInstanceState)?我不明白这个捆绑包将如何恢复信息.如果有人可以提供一个例子,那将会很有帮助.开发指南没有很好地解释这一点.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Spi*_*idy 183

Bundle是您要保存的所有信息的容器.您可以使用put*函数将数据插入其中.这是一个简短的列表(还有更多)可以用来存储数据的put函数Bundle.

putString
putBoolean
putByte
putChar
putFloat
putLong
putShort
putParcelable (used for objects but they must implement Parcelable)
Run Code Online (Sandbox Code Playgroud)

在您的onCreate功能中,这Bundle将被传回给程序.检查应用程序是重新加载还是第一次启动的最佳方法是:

if (savedInstanceState != null) {
    // Then the application is being reloaded
}
Run Code Online (Sandbox Code Playgroud)

要恢复数据,请使用get*函数,就像put*函数一样.数据存储为名称 - 值对.这就像一个hashmap.你提供了一个键和值,然后当你想要返回值时,你给出键,函数得到值.这是一个简短的例子.

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

您保存的信息将被烘烤到屏幕上.希望这可以帮助.

  • 在您的活动暂停之前调用onSaveInstanceState().因此,可以从保存的Bundle中检索它可能被破坏后所需的任何信息 (14认同)
  • @Diederik - 好的补充. (2认同)

Kei*_*ler 33

所有新Android开发人员应该知道的一个主要注意事项是,只要您为其分配ID,Widgets(TextView,Buttons等)中的任何信息都将由Android自动保留.这意味着大多数UI状态都会得到解决而没有问题.只有当您需要存储其他数据时才会出现问题.

来自Android文档:

您需要的唯一工作是为要保存其状态的每个窗口小部件提供唯一的ID(带有android:id属性).如果窗口小部件没有ID,则无法保存其状态

  • 如果你的目标是保存信息onSaveInstanceState不是这样做的地方.那是因为无法保证它会被调用(参见文档).您应该写入数据库,SharedPreferences等.是的,这些信息是准确的.测试UI是否以这种方式持久化的最佳方法是在方向更改时重新运行onCreate并使用bundle恢复状态来旋转显示. (2认同)

Die*_*uza 6

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


Kla*_*hen 5

储存信息:

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);
}
Run Code Online (Sandbox Code Playgroud)

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

以下是示例:重新创建活动

您可以选择实现 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);
}
Run Code Online (Sandbox Code Playgroud)