[英]Activity onCreate and onResume not called
I am working on an android project. 我正在做一个android项目。 There is a MainActivity and a Service. 有一个MainActivity和一个服务。 When user press back key twice in a given time period within MainActivity, I use the following to finish the activity and stop the service. 当用户在MainActivity中的给定时间段内两次按返回键时,我将使用以下方法完成活动并停止服务。
finish();
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
When I press the app icon to restart the app, MainActivity appears and Service restarts. 当我按下应用程序图标以重新启动应用程序时,MainActivity出现,并且服务重新启动。 However, onCreate and onResume in the MainActivity is not called. 但是,不会调用MainActivity中的onCreate和onResume。 The first call in the log is onAttach for fragments. 日志中的第一个调用是onAttach以获取片段。
How could that be possible? 那怎么可能呢?
You should not "kill" this by design. 您不应通过设计“杀死”它。
Call 呼叫
finish() and stopService() or stopSelf()
for your Service. 为您服务。
Killing the process may lead into unusual behaviour since the whole app gets restarted. 由于重新启动整个应用程序,因此终止进程可能会导致异常行为。 It may be possible that even if the "App" is killed that the view is still in memory. 即使“应用程序”被杀死,该视图仍可能在内存中。
Check your Mainactivity onPause()
and onDestory()
检查您的主要活动onPause()
和onDestory()
If your onDestory()
called then you onCreate()
and onResume() Definitely Called 如果您的onDestory()
调用了,那么您肯定会调用onCreate()
和onResume()
Note: onDestory is a risky one. 注意:onDestory是有风险的。 If the app killed by Android for memory allocation most of the time onDestory won't get called. 如果大多数情况下,Android终止了该应用程序的内存分配,则不会调用onDestory。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.