如何设置Bundles并获取数据

时间:2012-07-21 17:36:12

标签: java android sdk

我想知道如何在bundle中保存变量,然后使用onResume()方法返回数据。

2 个答案:

答案 0 :(得分:0)

您需要覆盖onSaveInstanceState:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("key", "Hello World");
}

要恢复数据,请覆盖onRestoreInstanceState(您的onCreate也会收到此Bundle):

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    String myString = savedInstanceState.getString("key");
}

答案 1 :(得分:0)

查看onSaveInstanceState()onRestoreInstanceState()的文档。这些是您的活动类的生命周期方法。