简体   繁体   English

我如何获得重新启动但不重新创建的活动?

[英]how do i get an activity to restart but not re-create?

Here is the scenario: 这是场景:

I have two Activities. 我有两个活动。 Lets name them Activity A and Activity B . 让我们将它们命名为Activity AActivity B

Say Activity A is open. Activity A已打开。 Now, when I go and open Activity B , Activity A is closed because the onStop() method is called. 现在,当我打开Activity B ,由于调用了onStop()方法,所以Activity A关闭了。

Now, when I flip back to Activity A , the onCreate() method is called, but I want the onRestart() method called instead. 现在,当我返回到Activity A ,将调用onCreate()方法,但我希望onRestart()调用onRestart()方法。 How do I do this? 我该怎么做呢?

You cannot influence the livecycle of your app like that. 您不能像这样影响应用程序的生命周期。 There should be no reason to rely on onRestart() . 应该没有理由依赖onRestart() If you use onStart() it will always be called no matter if the Android OS killed the app process in the background. 如果您使用onStart() ,则无论Android操作系统是否在后台终止应用程序进程,都将始终调用它。

Check out this doc for further information: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle 请查看此文档以获取更多信息: http : //developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Damn beat me to it but here goes anyway 该死的打败了​​我,但是反正

According to the Activity Lifecycle onCreate() is called again if the Activity was removed from memory because the OS deemed that another app needed the memory. 根据活动生命周期 ,如果由于操作系统认为另一个应用程序需要内存而将活动从内存中删除,则会再次调用onCreate() In this case, you can't ensure that onRestart() will always be called for your Activity. 在这种情况下,您不能确保始终为您的Activity调用onRestart()

Like already stated you must find a different way of achieving your goal by using the other Lifecycle methods such as onStart() or onResume 如前所述,您必须通过使用其他生命周期方法(例如onStart()onResume onStart()找到另一种实现目标的方法

I'm not sure if it fits your needs, I had to do an update service that starts the first time I open ActivityA (main Activity) and stops when exiting from ActivityA (not returning back from ActivityB), 我不确定它是否满足您的需求,我必须做一个更新服务,该服务在我第一次打开ActivityA(主Activity)时启动,而在从ActivityA退出(而不是从ActivityB返回)时停止,

I've placed the "start code" in onCreate() when savedInstanceState is null and the "stop code" in onDestroy() if isFinishing() is true 如果isFinishing()为true,则将“开始代码”放置在onCreate()savedInstanceStatenull ,将“停止代码”放置在onDestroy()

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

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