保存活动状态(而不仅仅是一些变量)的方向变化 [英] Save activity state(not just some variables) on orientation change

查看:162
本文介绍了保存活动状态(而不仅仅是一些变量)的方向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到,有很多的问题,关于这个话题已经问过的SO。 但是,我甚至不知道基本的,当涉及到的节约活动的状态

  

(参见下面的截图)当应用程序启动,

1)滚动型项目1,2,3,4是可见的。

2)的填充,由于领涨表containd数据按钮。

  

作为显示在下面的截图,虽然应用程序是在人像模式下运行

1)向下滚动滚动型项目4,5,6

2)$ P $下面的按钮变化表pssed失败者按钮,相应的数据。

3)我甚至可以动态改变图形的内容(我还没有完成)。

  

现在,我切换到横向模式

1)滚动型是出滚动型项目1,2,3,4

2)表中所示的是填充由于pressing获得者按键数据。

3)图是因为它是因为我还没有改变它,但(我将在以后改变)。

  

那么,什么情况是,当我改变方向,我的活动得到重新启动。因此,如果用户正在执行一些任务在一个方向和他改变的定向,然后整个进展将会丢失。

我知道我需要保存活动的状态,而当定向改变恢复。 但我不知道从哪里开始以及如何保存。

任何帮助将生活金丹!

解决方案

选项#1:覆盖的onSaveInstanceState()活动的,把任何信息要在提供的捆绑。您的新活动实例会收到捆绑 onRestoreInstanceState()(或的onCreate())。 下面是一个示例项目演示此

选项2:覆盖 onRetainNonConfigurationInstance()活动的键,返回重新presents某个对象你的状态。您的新活动实例可以调用 getLastNonConfigurationInstance()检索对象,因此新的活动可以应用这些信息。要小心,虽然,不是在保存的参考回到旧的活动对象的老年活动回报的东西(比如,一个小部件,一个普通内部类的一个实例)。 下面是一个示例项目演示此

选项#3:这个活动转换为一个片段。有段呼叫 setRetainInstance(真); 本身在其初始设置。通过 FragmentTransaction 动态添加片段的一些活动。现在,当配置更改,片段被保留,所以所有的部件和状态的保持。 下面是一个过于复杂的示例应用程序展示了这一

这些都是时下三个推荐的方法。

I realized that there are lots of question on this topic already asked on SO. But I don't even know the basic when it comes to saving the state of an activity.


(Refer Screenshot Below) When app launches,

1) ScrollView item 1,2,3,4 are visible

2) table containd data which is populated due to Gainer button.


As showed in below screenshots, While app is running in PORTRAIT mode, I

1)scrolled down to ScrollView item 4,5,6

2)pressed the Loser button so accordingly data in the table below the button changes.

3)I'll even change content of graph dynamically(which I had not done yet).


Now I switch to LANDSCAPE Mode so

1)ScrollView is showing ScrollView item 1,2,3,4

2)table is showing data which is populated due to pressing Gainer button.

3)graph is as it is as I've not changed it yet(which I will change later).


So what happens is when I change the orientation, my activity is getting re-launched. So if user is performing some task in one orientation and he changes the orientation, then whole progress will be lost.

I know I need to save the state of the activity and Restore it when orientation changes. But I don't know from where to start and what to save.

ANY HELP WILL BE LIFE-SAVER !

解决方案

Option #1: Override onSaveInstanceState() of your Activity and put whatever information you want in the supplied Bundle. Your new activity instance will receive that Bundle in onRestoreInstanceState() (or onCreate()). Here is a sample project demonstrating this.

Option #2: Override onRetainNonConfigurationInstance() of your Activity and return some object that represents your state. Your new activity instance can call getLastNonConfigurationInstance() to retrieve that object, so the new activity can apply that information. Be careful, though, not to have the old activity return something in the object that holds a reference back to the old activity (e.g., a widget, an instance of a regular inner class). Here is a sample project demonstrating this.

Option #3: Convert this activity to a fragment. Have the fragment call setRetainInstance(true); on itself during its initial setup. Add the fragment dynamically to some activity via a FragmentTransaction. Now, when the configuration changes, the fragment is retained, so all your widgets and state are retained. Here is an overly-complex sample application demonstrating this.

Those are the three recommended approaches nowadays.

这篇关于保存活动状态(而不仅仅是一些变量)的方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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