1 回答

TA貢獻1906條經驗 獲得超10個贊
首先,以onCreate.以外的其他方法訪問 SharedPreferences 是完全有效的。如果您在這篇文章中遇到一些問題,請查看錯誤消息/問題。
其次,為了在屏幕旋轉時保留內容,您可以onSaveInstanceState在 Activity 中使用 來保存微調器選擇并onCreate在屏幕旋轉時獲得狀態。例如:
private int saved_selection = -1
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
if (savedInstanceState != null) {
saved_selection = savedInstanceState.getInt("SPINNER_SELECTION");
}
//...
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt("SPINNER_SELECTION", saved_selection);
// call superclass to save any view hierarchy
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//...
// set `saved_selection = i;` in your adapter
//...
if( saved_selection >= 0 ) {
mSpinner.setSelection(saved_selection);
}
}
添加回答
舉報