简体   繁体   English

为什么当我们关闭应用程序时在android中只调用主要活动ondestroy

[英]why only main activity ondestroy is called in android when we close the app

My android application consists of many activities.我的 android 应用程序包含许多活动。 The very first activity that is called when app is opened is Login activity.打开应用程序时调用的第一个活动是登录活动。 After that the user navigates to many activities.之后,用户导航到许多活动。 But when the user close the app, only the onDestroy method of Login activity is called.但是当用户关闭应用程序时,只会调用 Login 活动的 onDestroy 方法。 Why does the android system calls only the first activity onDestroy, not others?为什么android系统只调用第一个活动onDestroy,而不调用其他活动? If the system is designed in such a way to call only first activity onDestroy, then can we expect that it will be called for sure everytime we close the app?如果系统设计为只调用 onDestroy 的第一个活动,那么我们是否可以期望每次关闭应用程序时都会调用它?

According to Android Develper Doc for onDestory()根据 Android 开发人员文档onDestory()

There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.在某些情况下,系统将简单地终止活动的托管进程,而不会在其中调用此方法(或任何其他方法),因此不应使用它来执行在进程消失后打算保留的事情。

I had a similar question.我有一个类似的问题。

In short, other activities are also closed altogether by the system, but their onDestroy() methods are not called.简而言之,系统也完全关闭了其他活动,但不会调用它们的onDestroy()方法。 They just get killed.他们只会被杀。 Only the main activity's onDestroy() gets called.只有主要活动的onDestroy()被调用。

read this, valeonom's comments:阅读本文,valeonom 的评论:

https://www.reddit.com/r/androiddev/comments/2s2hqh/whats_the_point_of_ondestroy_if_we_can_never/ https://www.reddit.com/r/androiddev/comments/2s2hqh/whats_the_point_of_ondestroy_if_we_can_never/

and this:和这个:

Why only the first activity's `onDestroy` is called, not other activities? 为什么只调用第一个活动的“onDestroy”,而不调用其他活动?

暂无
暂无

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

相关问题 onDestroy() 在活动横向模式 android 时调用 - onDestroy() called when a in activity landscape mode android 并非总是调用Android Activity onDestroy(),如果调用,则仅执行部分代码 - Android Activity onDestroy() is not always called and if called only part of the code is executed 为什么在切换到另一个应用程序时调用 onDestroy() - why is onDestroy() called when switching to another app 当我从main启动活动时,将调用main的onCreate(),并在启动活动完成后调用onDestroy() - When I launch an activity from main, onCreate() of main is called, and when the launched activity is finished onDestroy() is called 调用Activity onDestroy时调用Android onCreate Service - Android onCreate Service called when Activity onDestroy called Android:当Dalvik杀死此活动时,不会调用Activity.onDestroy() - Android: Activity.onDestroy() is not called when Dalvik kills this Activity Android:从最近使用的应用程序按钮关闭应用程序时未调用OnDestroy - Android: OnDestroy isn't called when I close the app from the recent apps button 当Android销毁活动以节省内存但不杀死App时,是否总是调用onDestroy? - Is onDestroy always called when android destroys activity to save memory but does not kill App? 为什么只调用第一个活动的`onDestroy`,而不调用其他活动? - Why only the first activity's `onDestroy` is called, not other activities? 当我单击通知并进入活动时,为什么调用onDestroy()? - Why is onDestroy() called when I click on a notification and go to my activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM