简体   繁体   English

Android:当应用程序被销毁并重新创建时总是启动启动活动

[英]Android: always launch start activity when app is destroyed and recreated

Lets say I have two activity, A and B. A - is my splash activity, and it also handles the initialization part.假设我有两个活动,A 和 B。A - 是我的启动活动,它还处理初始化部分。 When i launch app, Activity A is started or launched and then it gets navigated to Activity B .当我启动应用程序时, Activity A被启动或启动,然后它被导航到Activity B Then i press home button , and wait for some time, for app to be destroyed.然后我按下home button ,等待一段时间,应用程序被销毁。 And i press my app's icon again, and my android (5.0) remebers what activity i stoped using, and launches it (activity B).然后我再次按下我的应用程序图标,我的 android (5.0) 会记住我停止使用的活动,并启动它(活动 B)。 and App crashes, since initialization was not occured.和应用程序崩溃,因为没有发生初始化。 How to force android to start my activity A in any case?在任何情况下如何强制 android 开始我的活动 A?

I had also tried android:clearTaskOnLaunch="true"我也试过android:clearTaskOnLaunch="true"

I had even tried to finish() activity if it is activity B, but anyway onCreate of fragments are called, and app crashes.如果它是活动 B,我什至尝试过finish()活动,但无论如何都会调用片段的 onCreate,并且应用程序崩溃。

Any help will be appreciated.任何帮助将不胜感激。 Thanks in advance提前致谢

Why don't you call finish(); 你为什么不打电话给finish(); on your Activity B's onStop() method. Activity B's onStop()方法上。 It could close your Activity B once the focus is off from the activity and now the app can start from Activity A again on relaunch. 一旦焦点离开活动,它可能会关闭您的Activity B ,现在应用程序可以在重新启动时再次从Activity A开始。

May be this can help you... 可能这可以帮到你......

Don't rely upon Activity A and B dependence.不要依赖 Activity AB依赖。 We do not have any control whether the OS decides to kill any of those.我们无法控制操作系统是否决定终止其中任何一个。 It might be due to battery limits or a need to free up memory.这可能是由于电池限制或需要释放 memory。

This can be also checked with 'Dont keep Activities' developer option.这也可以通过“不保留活动”开发人员选项进行检查。

What I suggest doing is adding an if-check logic in Activity B to use the init config or init it, if not possible.我建议做的是在 Activity B 中添加一个 if-check 逻辑以使用 init 配置或初始化它,如果不可能的话。

Then, you will be prepared for the case of Android killing your activity (rare case than A -> B flow, but possible).然后,您将为 Android 杀死您的活动的情况做好准备(比A -> B流程少见,但有可能)。

Hence, Activity logic coupling is a no-go.因此,Activity 逻辑耦合是不行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM