在方向改变时保存活动状态(不仅仅是一些变量)
我意识到关于这个话题已经有很多问题了。 但我什至不知道保存活动状态的基本知识。
<小时>
(请参阅下面的屏幕截图)当应用程序启动时,
1)ScrollView 项目 1,2,3,4 可见
2)表格包含由于 Gainer 按钮而填充的数据。
<小时>
如下面的屏幕截图所示,当应用程序在 PORTRAIT 模式下运行时,我
1) 向下滚动到 ScrollView 项目 4,5,6
2) 按下了 Loser 按钮,因此按钮下方表格中的数据相应发生变化。
3)我什至会动态更改图表的内容(我还没有这样做)。
<小时>
现在我切换到横向模式,因此
1)ScrollView 显示 ScrollView 项目 1,2,3,4
2)表显示由于按下 Gainer 按钮而填充的数据。
3)图表是因为我还没有改变它(我稍后会改变)。
<小时>
因此,当我改变方向时,我的活动就会重新启动。因此,如果用户在一个方向上执行某项任务并且改变了方向,那么整个进度将会丢失。
我知道我需要保存活动的状态并在方向改变时恢复它。 但我不知道从哪里开始,也不知道该保存什么。
任何帮助都可以挽救生命!
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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
选项#1:覆盖
Activity
的onSaveInstanceState()
并将您想要的任何信息放入提供的Bundle
中。您的新 Activity 实例将在onRestoreInstanceState()
(或onCreate()
)中接收该Bundle
。 这是一个演示这一点的示例项目。选项#2:覆盖
Activity
的onRetainNonConfigurationInstance()
并返回一些代表您状态的对象。您的新 Activity 实例可以调用getLastNonConfigurationInstance()
来检索该对象,以便新 Activity 可以应用该信息。但要小心,不要让旧活动返回对象中的某些内容,该对象保留对旧活动的引用(例如,小部件、常规内部类的实例)。 这是一个演示这一点的示例项目。选项#3:将此活动转换为片段。让片段在初始设置期间自行调用
setRetainInstance(true);
。通过 FragmentTransaction 将片段动态添加到某些 Activity。现在,当配置更改时,片段将被保留,因此所有小部件和状态都会被保留。 这是一个过于复杂的示例应用程序,演示了这一点。这些是当今推荐的三种方法。
Option #1: Override
onSaveInstanceState()
of yourActivity
and put whatever information you want in the suppliedBundle
. Your new activity instance will receive thatBundle
inonRestoreInstanceState()
(oronCreate()
). Here is a sample project demonstrating this.Option #2: Override
onRetainNonConfigurationInstance()
of yourActivity
and return some object that represents your state. Your new activity instance can callgetLastNonConfigurationInstance()
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 aFragmentTransaction
. 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.