如何刷新无需重新启动活动在Android RelativeLayout的时候方向更改?重新启动、方向、时候、RelativeLayout

2023-09-04 06:43:33 作者:先森别耍帅我有老公

我有一个Android活动与RelativeLayout的,我已经被重新定位上的变化采取了以下方法prevent活动:

I have an Android Activity with a RelativeLayout and I have implemented the following method to prevent the activity from being recreated on change of Orientation:

@Override
public void onConfigurationChanged(Configuration newConfig) {       
    super.onConfigurationChanged(newConfig);

}

我显然不是在做这个事情的方法,但它使用的LinearLayout时,工作完美。但是现在,使用RelativeLayout的,我的布局全部更换为横向时搞砸了。

I am obviously not doing anything in this method, but it worked perfect when using a LinearLayout. Now however, using RelativeLayout, my layout is all messed up when changing to landscape orientation.

什么是有屏幕重绘正确不具有活性调用的OnCreate再次重启?最有效的方式。

What is the most efficient way to have the screen redraw correctly without having the activity restarted again with a call to onCreate?

推荐答案

您有两种方式:

在保留对象在一个配置更改 处理配置改变自己

您已经选择了第二种方式。在这种情况下,你必须重新分配资源的每手而这种做法是不推荐。

You have chosen the second way. In this case you have to reassign every resource by hand and this approach is not recommended.

IMHO最好的方式是在第一通过实施该方法:onRetainNonConfigurationInstance();

IMHO the best way is the first by implementing the method: onRetainNonConfigurationInstance();

这里,了解如何使用它们的完整例子。

Here a complete example on howto use.

注:的onCreate将会再次调用,但你可以调整你的code至prevent长捉迷藏的任务要在配置更改时再次呼吁

Note: the onCreate will be called again but you can adjust your code to prevent long runnning task to be called again during a configuration change