覆盖方向改变,但没有重新启动活动,并通过国家数据 [英] Override Orientation Change But NOT Restart The Activity AND Pass State Data

查看:127
本文介绍了覆盖方向改变,但没有重新启动活动,并通过国家数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够改变布局当设备被重新定位到横向或纵向。对于速度和资源的目的(加上适用于我的应用程序等问题),我不希望我的应用程序被破坏并重新启动。我有我希望的方向变化的保留,因为没有毁坏并重新创建这些好处几个对象!我只是只想让他们适应当前的方向改变一些按钮和TextViews的位置。容易吧?

I want to be able to change the layout when a device is re-orientated to landscape or portrait. For speed and resource purposes (plus other issues applicable to my app) I do NOT want my app to be destroyed and restarted. I have several objects which I wish to retain between orientation changes as there is no benefit from destroying and re-creating them! I simply just want to change the position of some buttons and TextViews so that they suit the current orientation. Easy right?

哦,不不是。为了实现上述我包含在应用清单定向变化configChange选项。然后,我已经实现了onConfigurationChanged(),我确定并应用适当的布局。简单是吗?

Well no it isn't. To achieve the above I included in the app Manifest the configChange option for orientation change. Then I've implemented the onConfigurationChanged() where I determine and apply the appropriate layout. Simple yes?

但现在采取的TextView我在我的布局。如何在地球上,采用响应方向改变这种特殊的方法,我把在previous的TextView相同的文字到新的TextView?没有实例数据传递到onConfigurationChanged()方法。也为一些按钮,它们可禁用或启用...我需要orienatation更改后知道这一点。

But now take the textview I have in my layout. How on earth, using this particular method of responding to orientation changes, do I put the same text in the previous textview to the new textview? No instance data is passed to onConfigurationChanged() method. Also for some of the buttons, they could be disabled or enabled... I need to know this after orienatation change.

如果我让Android的破坏并重新启动我的活动它首先要创建不必要的工作。所有我想要的就是正义之举几个按钮,textviews ..不重新启动整个应用程序。这只是可笑!

If I let Android destroy and restart my activity it's first going to create unnecessary work. All I want is to just move a few buttons and textviews.. NOT restart the whole app. That's just ludicrous!

谁能帮我达到什么样的需要?

Can anyone help me achieve what need?

推荐答案

这是简单的方法来维护配置无关的数据是利用<一个href=\"http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29\"相对=nofollow> onRetainNonConfigurationInstance()及其配套方法<一href=\"http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance%28%29\"相对=nofollow> getLastNonConfigurationInstance()。只是返回一个包含所有你想重用,当你活动重新创建数据的对象。

An easy way to maintain configuration-independent data is to make use of onRetainNonConfigurationInstance() and its companion method getLastNonConfigurationInstance(). Just return an object that contains all the data that you want to reuse when your activity is recreated.

在蜂窝,或者,如果您使用的是Android的兼容包,你可以叫<一个href=\"http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance%28boolean%29\"相对=nofollow> Fragment.setRetainInstance(真)代替。请参阅该文档。

In Honeycomb, or if you are using the Android compatibility package, you can just call Fragment.setRetainInstance(true) instead. See the docs.

这篇关于覆盖方向改变,但没有重新启动活动,并通过国家数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆