English 中文(简体)
RetainTaskState始终无法按预期工作。Singleton类正在失去其状态
原标题:alwaysRetainTaskState doesn t work as expected. Singleton class is losing its state

在我的应用程序第一次运行时,在根活动上,用户需要选择一个特定的选项,以确定将从数据库中为他下载哪些数据。

一旦他选择了这个选项,数据就会被下载并保存在一个单例类中,只要应用程序在运行,该类就应该保存这些数据。(即使在后台)

我的问题是,有时在用户退出我的应用程序一段时间后(使用主页按钮),Android显然会杀死我应用程序中的一些活动,并以某种方式重置重要的单例类。一旦我尝试访问应该保存在单例上的数据,就会导致我的应用程序收到各种空引用异常。

我知道Android操作系统有时可以选择终止应用程序,但允许用户从上次访问的活动中返回。有时它只会杀死整个应用程序,并迫使用户从头开始。

我一直在寻找解决方案,我发现了“android:always RetainTaskState”属性,我可以将其应用于我的根活动。显然,有了它,每当android决定终止我的应用程序时,它至少会迫使用户从我可以重新下载数据的第一个活动开始,而不是允许用户从更高级的活动开始,导致他得到空异常。

然而,当我在应用程序上应用它时,它不起作用,当我返回应用程序时,我从一个高级活动开始,只有一个空的单例实例。

我还尝试了“android:clearTaskOnLaunch”,它奏效了,但这有点过头了,因为我不希望用户每次回到应用程序时都必须重新开始。我希望只有当安卓决定终止该应用程序时,才会发生这种情况。

我做错了什么吗?是否有可能有更好的解决方案来保持单胎存活?

谢谢

最佳回答

不。没有更好的解决方案。这就是它的工作原理。Android可以随时终止应用程序的进程(只要它在后台)。你可以将数据保存在单例中,但无论如何,Android都有可能杀死你的进程。当用户返回到您的应用程序时,Android将创建一个新进程,然后创建用户正在恢复的活动的新实例。您需要将代码放入所有活动的onCreate()中,以识别您的单例已消失,然后执行适当的操作(重定向到应用程序的根活动或重新初始化单例。

很容易判断你的进程是否已被杀死并重新创建。只需在单例中添加一个静态布尔变量,并在初始化时将其设置为trueonCreate()中,检查该变量的状态,如果该变量为false

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...