android横竖屏切换与数据保存
一,横竖屏切换时不重新载入数据,只需在menifest中加入:
android:configChanges="keyboardHidden|orientation"
二,横竖屏切换时重新载入数据,程序会程序进入onCreate,因此需要对数据进行存储以备后用。
***方法一:
@Override
public Object onRetainNonConfigurationInstance() {
// save data :browserHistoryAdapter
Intent i = new Intent();
Bundle b = new Bundle();
b.putSerializable(KEY_HISTORY_SAVED, (Serializable) browserHistoryAdapter);
i.putExtras(b);
return i;
}
// get saved data after configure changed :browserHistoryAdapter
Intent historySaved;
if((historySaved = (Intent) getLastNonConfigurationInstance()) == null){
historySaved = getIntent();
}
if(historySaved != null && historySaved.getExtras() != null){
browserHistoryAdapter = (BrowserHistoryAdapter) historySaved.getExtras().get(KEY_HISTORY_SAVED);
}
***方法二:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save data :url
outState.putString(STR_URL, url);
}
// get saved data after configure changed :url
if (savedInstanceState != null) {
url = savedInstanceState.getString(STR_URL);
}
摘自 fhy_2008的专栏
补充:移动开发 , Android ,