如何节约活动的样子 [英] how to save state of activity

查看:116
本文介绍了如何节约活动的样子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来android.i有一个单一的活动的main.xml 文件。现在,我有一个滚动视图 的main.xml file.when我跑我的应用程序肖像模式,当我转到滚动视图的底部,当我改变我的应用程序状态不是<当我们纵向之间切换,以的景观模式,比我去滚动view..that顶部手段>景观或反之亦然活动被调出。因此需要保存应用程序状态纵向景观恢复。所以它的任何解决方案?
提前致谢
阿米尔·罕我。

i am new to android.i have one single activity with main.xml file. Now, i have one scroll view in that main.xml file.when i run my application in portrait mode and when i go to the bottom of the scroll view and than when i change my application state to the landscape mode than i go to the top of the scroll view..that means when we switch between portrait to landscape or vice versa the activity is recalled. so need to save the application state in portrait and restore in landscape. so any solution of it? thanks in advance Aamirkhan I.

推荐答案

的文档做的解释不同的方式来处理配置更改,包括屏幕方向的变化。其中的一个方法,这是很好的保存UI的临时状态,在 onSavedInstanceState()方法保存数据 - 如@Jason匡提到

The documentation does a decent job of explaining different ways to handle configuration changes, including screen orientation changes. One of those methods, which is good for saving temporary state of the UI, is saving data in the onSavedInstanceState() method--as @Jason Kuang mentioned.

一般情况下,你可以依靠Android的保存和恢复的查看 S中的状态,您无需任何特殊的努力。该人士$ ​​C $ C为保护方法的onSaveInstanceState()解释(强调):

Generally, you can rely on Android to save and restore the state of Views without any special effort on your part. The source code for the protected method onSaveInstanceState() explains (emphasis added):

默认实现通过调用android.view.View.onSaveInstanceState()上有一个id层次的每个视图,然后通过保​​存的ID需要照顾大多数UI每个实例的状态为您目前主要集中视图(所有这些由onRestoreInstanceState(android.os.Bundle)的默认实现恢复)。如果覆盖此方法来保存不受每个单独的视图捕获更多的信息,你可能会想通过打电话的默认实现,否则会ppared保存所有每个国家的看待自己$ P $。

The default implementation takes care of most of the UI per-instance state for you by calling android.view.View.onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(android.os.Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.

这是一个有点欺骗性,因为API文档指出的EditText 的TextView 取值必须具有的android:freezesText =真正的明确宣布他们在您的布局XML文件,以确保Android的自动保存其状态时,的onSaveInstanceState()被调用。我最近没有测试过这一点,但它是源$ C ​​$ C,似乎有什么做的事情。因此,在你自己处理临时UI状态是最好的。

This is a little deceptive, because the API documentation states that EditTexts and TextViews must have android:freezesText="true" explicitly declared on them in your layout XML files to ensure that Android automatically stores their state when onSaveInstanceState() is invoked. I have not tested this recently, but it is what the source code seems to be doing. Therefore, handling temporary UI state on your own is best.

另一个提示:你可以明确地prevent临时数据的存储查看致电 setSaveEnabled(假)查看。 (这不会影响它的孩子。)

Another tip: You can explicitly prevent the storage of temporary data for a View by calling setSaveEnabled(false) on that View. (This will not affect its children.)

作为一项规则,这是一个好主意,手动​​保存在你的的onPause()方法屏幕上的状态,也是在的onSaveInstanceState( )

As a rule, it's a good idea to manually save the on-screen state in your onPause() method, and also in onSaveInstanceState().

这篇关于如何节约活动的样子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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