简体   繁体   English

未调用活动onCreate和onResume

[英]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.

相关问题 当应用程序将用户引导至活动时调用 onCreate 和 onResume - onCreate and onResume were called when app direct user to an activity onResume调用了noHistory活动 - onResume called on noHistory activity 是否保证调用活动的 onResume? - Is onResume of activity guaranteed to be called? 在活动A之上启动活动B(由应用程序类的子类的onCreate调用)(由活动类的子类的onResume调用) - start Activity B(called by onCreate of an Application Class's sub Class) on Top of Activity A(called by onResume of an Activity Class's sub Class) 在物理设备中未调用onResume(),而是调用了onCreate() - onResume() is not called in physical device instead onCreate() is called 如果应用程序闲置了数小时并被OS破坏,是否在Activity.onResume之前调用Application.onCreate? - Is Application.onCreate called before Activity.onResume if app has been idle for hours and destroyed by OS? Android在onCreate中保存微调器状态,并在onResume中保存活动 - Android Save spinner state in onCreate, and Activity onResume Android Studio活动-使用onCreate(),onResume()等 - Android Studio activity - using onCreate(), onResume() etc 我的活动在 onResume() 之前调用 onCreate() - My activity invokes onCreate() before onResume() Android:onResume 总是在 onCreate 之后调用吗? - Android: Is onResume always called after onCreate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM