Android的 - 保存活动状态开始新的活动时, [英] Android - Save activity state when starting new activity

查看:205
本文介绍了Android的 - 保存活动状态开始新的活动时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作应该有以下行为的应用程序: 在活动中的一个,用户可以选择不同的项放入列表。虽然这样做,他应该能拍张照片,将其保存,并返回到previous活动。

我有以下问题:当用户返回到活动拍摄照片,进度(基本上,选择的项目)都将丢失后。

我试图用机器人:launchMode =singleInstance(以及singleTask)和android:alwaysRetainTaskState =真正的我的活动,以保存其当前状态(和它的伟大工程,当用户离开应用程序做一些事情否则,再后来回来吧)。我试着吃午饭的FLAG_ACTIVITY_NEW_TASK模拟相同的行为ACTION_IM​​AGE_CAPTURE意图(地方在我心目中,我认为这应该是同样的事情,吃午饭,从主屏幕上的画面活动),但没有效果。我甚至尝试吃午饭的图片活性startActivityforResult,希望,不知怎的,我的活动不会丢失,但仍然没有效果。

我知道我可以使用捆绑savedInstanceState手动保存,并通过覆盖在onPause,onResume恢复状态,上创建(等),但实际上大量的自定义组件是不容易序列化的数据,所以我想避免这种情况。

嗯,我想我的问题是我怎么能重现相同的行为alwaysRetainTaskState清单属性?或者,反正是有轻松保存我的活动状态?或者我究竟做错了,错过了很明显?

非常感谢!

解决方案
  

我知道我可以使用捆绑savedInstanceState手动保存,并通过覆盖在onPause,onResume恢复状态,上创建(等),但实际上大量的自定义组件是不容易序列化的数据,所以我想避免

你会喜欢并不重要。你需要这个code反正来处理配置更改(例如,交换机从纵向到横向)。请执行的onSaveInstanceState() onRestoreInstanceState(),然后背出来,你在不恰当的黑客所有的休息您的previous段(例如, singleInstance alwaysRetainTaskState )。

I'm currently working an application that should have the following behavior : In one of the activities, the user can select different items into a list. While doing that, he should be able to take a picture, save it, and return to the previous activity.

I'm having the following problem : when the user goes back to the activity after taking the picture, the progress (basically, the items selected) is lost.

I tried to use android:launchMode="singleInstance" (as well as singleTask) and android:alwaysRetainTaskState="true" on my activity to save its current state (and it works great when the user leave the application to do something else, then come back later to it). I tried to lunch the ACTION_IMAGE_CAPTURE intent with the FLAG_ACTIVITY_NEW_TASK to simulate the same behavior (somewhere in my mind i thought it should be the same thing as lunching the picture activity from the homescreen), but with no effect. I even tried to lunch the picture activity with startActivityforResult with the hope that somehow my activity wouldn't be lost, but with still no effect.

I know I could use the Bundle savedInstanceState to manually save and restore the state by overriding onPause, onResume, on Create (etc), but there is actually lots of data in custom components that are not easily serializable, so I'd like to avoid that.

Well I guess my question is how can I reproduce the same behavior as alwaysRetainTaskState manifest attribute? Or is there anyway to easily save the state of my activity? Or what am I doing wrong, missing the obvious?

Thanks a lot!

解决方案

I know I could use the Bundle savedInstanceState to manually save and restore the state by overriding onPause, onResume, on Create (etc), but there is actually lots of data in custom components that are not easily serializable, so I'd like to avoid that.

What you would "like" does not much matter. You need this code anyway to handle configuration changes (e.g., switch from portrait to landscape). Please implement onSaveInstanceState() and onRestoreInstanceState(), then back out all the rest of the inappropriate hacks that you have in your previous paragraph (e.g., singleInstance, alwaysRetainTaskState).

这篇关于Android的 - 保存活动状态开始新的活动时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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