重新启动应用程序 - 活动入口点 [英] Application restart - Activity Entry Point

查看:239
本文介绍了重新启动应用程序 - 活动入口点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中的入口点是让我们说A 登录/扑通 活动,在这里我需要pre从服务器-load新数据。这 SplashActivity 被声明为:

I have an application where the entry point is let's say a "login/splash" Activity, where I need to pre-load fresh data from the server. This SplashActivity is declared as :

<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

在我的的Andr​​oidManifest.xml ,加载数据后,我一直在我的自定义的一些数据应用类和我继续我的 MainActivity

in my AndroidManifest.xml, after data is loaded, I keep some data on my custom Application class and I proceed to my MainActivity.

我期待,我的应用程序后,由操作系统或用户(使用强制停止)停止,然后再由用户重新开始,我的应用程序的入口点是 SplashActivity 再次 BUT ,系统将跳过 SplashActivity 和显示 MainActivity

I am expecting, that after my Application is stopped by the OS or by the user (using Force Stop), and then later is re-started by the user, the entry point of my application to be SplashActivity again BUT the system skips the SplashActivity and displays the MainActivity.

问:这是预期的行为?如果整个过程停止,不要我的应用程序与 SplashActivity 开始了吗?这能实现呢?

QUESTION: is this the expected behaviour? If the entire process is stopped, shouldn't my application be started with SplashActivity? Can this be accomplished?

推荐答案

其实,有由这个问题,并解决一些问题一些问题的答案吧:

Actually, there are several problems that are addressed by this question and some of the answers to it:

要回答你原来的问题,是的,这是预期的行为。

To answer your original question, "Yes, this is the expected behaviour".

Android的认为每一个活动是一个单独的独立的实体。 Android的记得在任务堆栈活动状态,它有没有问题杀死你的过程中(其中包含所有的活动),只要它想,是因为它知道它总是可以重建你的活动时,它需要的地方。这个概念,当然,坏了,当你有一个复杂的应用程序,你必须的活动和/或您有在应用类存储全局数据(或之间的依赖关系类似的静态/单位)。

Android considers each Activity to be a separate self-contained entity. Android remembers the state of activities in the task stack and it has no problem killing your process (which contains all your activities) whenever it wants to, because it "knows" that it can always reconstruct your activities whenever it needs to. This concept, of course, breaks down when you have a complex application where you have dependencies between the activities and/or you have global data that you store in an Application class (or similar static/singleton place).

在Android的杀死你的过程中,它会记住最上面的活动任务,当用户返回到它重新创建过程,然后重新创建任务的最上面的任务活动。在你的情况, MainActivity

When Android kills your process it remembers the topmost activity in the task and when the user returns to the task it recreates the process and then recreates only the topmost activity in the task. In your case, MainActivity.

作为一个例子,如果你的任务堆栈看起来是这样的:

As an example, if your task stack looks like this:

StartActivity -> ActivityB -> ActivityC -> ActivityD

和你的任务去后台和Android杀死的过程中,只有当用户返回到任务 ActivityD 将被重新创建。一旦 ActivityD 完成后,Android将重新创建 ActivityC 。一旦 ActivityC 完成后,Android将重新 ActivityB 等等,总之,完整的堆栈的不是当用户将恢复重建任务

and your task goes to the background and Android kills the process, when the user returns to the task only ActivityD will be recreated. Once ActivityD is finished, Android will then recreate ActivityC. Once ActivityC is finished, Android will recreate ActivityB, etc. In short, the complete stack is not recreated when the user resumes the task.

有没有舱单设置或意图的标志,将让你你想要的行为相结合。这将是很好,如果Android的提供类似的东西,但目前没有。

There is no combination of manifest settings or Intent flags that will get you the behaviour that you want. It would be nice if Android offered something like that, but at the moment it does not.

您可以判断,如果你的过程已经在应用程序中的派生类使用静态(类)布尔变量重新启动(或任何其他类)。这个变量将始终具有值当进程重新启动,然后你可以从任何地方和重新初始化(重新加载数据),如果有必要检查变量的状态。然后,设置变量真正。它将保持真正,直到该进程被杀害和重建,即使所有的活动结束。这样在需要的时候你只能初始化。

You can determine if your process has been restarted by using a static (class) boolean variable in your Application-derived class (or in any other class). This variable will always have the value false when the process is restarted and you can then check the state of the variable from anywhere and reinitialize (reload your data) if necessary. Then you set the variable to true. It will remain true until the process is killed and recreated, even if all your activities finish. In this way you can initialize only when needed.

您还可以使用此作为一项指标来重新启动从闪屏应用程序。因此,在所有的活动,在的onCreate(),您可以检查这个布尔变量的状态,如果该应用程序已重新启动,你可以简单地重定向到闪屏是这样的:

You can also use this as an indicator to restart your application from the SplashScreen. So in all your activities, in onCreate(), you can check the state of this boolean variable and if the application has been restarted you can simply redirect to the SplashScreen like this:

Intent intent = new Intent(this, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

这将完成任务的所有活动,并重新启动闪屏在任务的根源。

This will finish all the activities in the task and restart the SplashScreen at the root of the task.

接下来,如果你想有prevent每一次下载数据的用户返回到应用程序(当它是在后台,后经AndroidOS杀害),你应该保存您在下载数据私有缓存区,可使用该应用程序重新启动时。这$ P不必重复下载数据,如果你的进程被终止并重新启动$ pvents。

Next, if you want to prevent having to download data every time the user returns to the application (when it was in the background and subsequently killed by the AndroidOS), you should store the data that you download in the private cache area and use that when the application is restarted. This prevents having to download the data repeatedly if your process is killed and restarted.

这个处理将是加载服务数据的另一种方式。如果你有一个服务在过程中运行,那么Android是不太可能杀死你的进程。你只需要确保您关闭服务当用户完成与应用程序。

Another way of dealing with this would be to load your data in a Service. If you have a Service running in your process then Android is less likely to kill your process. You just need to make sure that you shut down your Service when the user is finished with your application.

我知道这个答案啰嗦。希望你能得到的东西出来。

I realize this answer is long-winded. Hopefully you can get something out of it.

这篇关于重新启动应用程序 - 活动入口点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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