空值,這是我在一類onCreate方法是片段savedInstanceState總是在的onCreate
static int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(savedInstanceState == null) { //made for first time
Log.d("came in", "Came in");
counter = 0;
} else {
counter = savedInstanceState.getInt("counter", 0);
}
}
的子類,這是我savedInstanceState方法
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
Log.d("hi there", ""+counter); // this is printing hi there 3
outState.putInt("counter", counter);//key value pair
}
但在onCreate方法savedInstanceState總是空並打印進來。
活動時已凍結狀態,即主要是在配置變化(旋轉等)保存的狀態只恢復了。沒有保存的狀態,當你從頭開始重新創建活動。你是否期望保存的狀態比現在更持久? – dhke
當我旋轉屏幕時,我想獲得計數器變量。 –