如何根据请求接收活动状态实例(包)?

时间:2017-01-03 12:04:00

标签: android android-activity activity-state

如何根据请求获取为此方法提供的包(Bundle outState)?

@Override
protected void onSaveInstanceState(***Bundle outState***) {
    super.onSaveInstanceState(outState);

    // Only if you need to restore open/close state when
    // the orientation is changed
    if (adapter != null) {
        adapter.saveStates(outState);
    }
}

3 个答案:

答案 0 :(得分:0)

它是您活动中Bundle方法中发送的OnCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        savedInstanceState.getString("bla");
    }
}

请注意,您必须检查null,因为第一次创建活动时它将为null,因为之前没有状态。

有关详细信息,请参阅https://developer.android.com/guide/components/activities/activity-lifecycle.html#oncreate

答案 1 :(得分:0)

您可以使用以下方法获取已保存的套装:

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) {
// get your saved bundles back here
} 

Just refer this developer page, you will get clear idea about this

答案 2 :(得分:0)

在Bundle实例中,您可以传递数据并将其返回onCreate()方法。喜欢以下

outState.putString("key1", "data1");
outState.putBoolean("key2", "data2");
outState.putInt("key3", "data3");

并在onCreate中获得如下

if (savedInstanceState != null){
            data_1 =  savedInstanceState.getString("keys1");
            data_2 =  savedInstanceState.getBoolean("keys2");
            data_3 =  savedInstanceState.getInt("keys3");

}