当 android:configChanges=“orientation” 时保存方向更改之前的状态已指定
我的 Android 应用程序中有一个 Activity
,它根据方向设置不同的布局 XML 作为其视图。我已在清单中声明了 android:configChanges="orientation"
。现在 onConfigurationChanged()
被调用 - 但此时新的方向已经生效。
我的目标是尝试融入生命周期并尝试在新方向生效之前保存一些更改;这样当我回到当前方向时就可以恢复状态。
我按如下方式破解了它,但我不确定这是否是正确的方法。我的过程包括将状态保存在 onConfigurationChanged()
中,然后调用 setContentView()
来设置新方向的布局。
public class SwitchOrientationActivity extends Activity {
private View mLandscape, mPortrait;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater li = LayoutInflater.from(this);
mLandscape = li.inflate(R.layout.landscape, null);
mPortrait = li.inflate(R.layout.portrait, null);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (Configuration.ORIENTATION_LANDSCAPE == newConfig.orientation) {
switchToLandscape();
} else {
switchToPortrait();
}
}
private void switchToPortrait() {
/*
* Use mLandscape.findViewById() to get to the views and save the values
* I'm interested in.
*/
saveLanscapeState();
setContentView(mPortrait);
}
private void switchToLandscape() {
/*
* Use mPortrait.findViewById() to get to the views and save the values
* I'm interested in.
*/
savePortraitState();
setContentView(mLandscape);
}
}
有没有更优雅的方法来实现这一点?
I have an Activity
in my Android app that sets different layout XMLs as its view depending on the orientation. I have declared android:configChanges="orientation"
in the manifest. Now onConfigurationChanged()
is called - but by this time the new orientation has already taken effect.
My goal is to try to hook into the life cycle and try to save some changes before the new orientation is in effect; so that I can restore the state when I return to the current orientation.
I hacked it as follows, but I'm not sure if this is the right way to do this. My procedure involves saving the state in onConfigurationChanged()
and then calling setContentView()
to set the layout for the new orientation.
public class SwitchOrientationActivity extends Activity {
private View mLandscape, mPortrait;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater li = LayoutInflater.from(this);
mLandscape = li.inflate(R.layout.landscape, null);
mPortrait = li.inflate(R.layout.portrait, null);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (Configuration.ORIENTATION_LANDSCAPE == newConfig.orientation) {
switchToLandscape();
} else {
switchToPortrait();
}
}
private void switchToPortrait() {
/*
* Use mLandscape.findViewById() to get to the views and save the values
* I'm interested in.
*/
saveLanscapeState();
setContentView(mPortrait);
}
private void switchToLandscape() {
/*
* Use mPortrait.findViewById() to get to the views and save the values
* I'm interested in.
*/
savePortraitState();
setContentView(mLandscape);
}
}
Is there a more elegant way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
android:configChanges="orientation"
导致您的 Activity 在方向更改时不会重新启动,因此会跳过常规生命周期。我建议您将其删除,然后实施 onRetainNonConfigurationInstance () 并在 onCreate 或 onRestoreInstanceState 中恢复您的状态。请参阅这篇文章了解更多信息android:configChanges="orientation"
causes your activity to not be restarted on an orientation change, and so skips the regular lifecycle for this. I'd suggest, you take that out, then implement onRetainNonConfigurationInstance() and restore your state in either onCreate or onRestoreInstanceState. See this article for more info