当按下某个按钮时,从activity_main开始的activity_main布局将更改为graphing。
public void onClick(DialogInterface dialog, int which) {
mode = selectedMode;
if (mode.equals("Graphing"))
{
setContentView(R.layout.graphing);
}
}但是,当用户处于该布局并更改方向时,graphing布局将返回到activity_main。令人不安的问题是,我对layout和layout-land都有必要的布局。
XML
layout
activity_main
graphing
layout-land
activity_main
graphing发布于 2015-07-30 19:02:08
为什么不尝试保存实例状态:
@Override
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
/*this line here save that the current orientation is landascepe */
state.putChar('O','L');
}然后检查该捆绑包是否是情感的,是否为空的,如果没有,则不会出现,如果没有,您将得到保存在budle中的内容,如下所示:
/* here you point to the id of the landscape layout */
int mode = R.layout.graphing;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
if ((bundle != null)
&& (bundle.getChar('O') != null)) {
View v=(View)findViewById(mode);
setContentView(v);
}
}您可以参考这些链接,它们非常有用:
http://developer.android.com/reference/android/os/Bundle.html
http://developer.android.com/reference/android/view/View.html
发布于 2015-07-30 19:17:59
“方向更改从onCreate开始抛出活动生命周期回调”,您将在onCreate中将布局设置为activity_main,当用户更改方向时,onCreate被触发,布局设置为activity_main。正如@Mhmd Salem所说,您可以使用public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)和protected void onRestoreInstanceState(Bundle savedInstanceState)保存和检索活动的状态。
https://stackoverflow.com/questions/31731691
复制相似问题