重新启动应用程序会从我关闭它的地方开始 - 不是从一开始就开始
我认为当我退出设备上的应用程序并且它在任务管理器中不再可见时,当我再次启动它时,该应用程序将从头重新启动。
但由于某种原因,我的应用程序保留了上次运行中的一些变量,并且并未真正正确重新启动。
仅当在设备本身上重新启动时才会发生这种情况。使用 Eclipse 重新启动会从一开始就正确启动应用程序并初始化所有变量。
这是为什么呢?我的假设是错误的,即退出应用程序时所有类和变量都将被销毁?
非常感谢
I thought when I exit the app on the device and it is not visible anymore in the taskmanager the app would restart from the very beginning when I start it again.
But for some reason my app keeps some variables from the previous run and does not really restart correctly.
This happens only if restarted on the device itself. Restarting with Eclipse starts the app correctly from the very start initializing all variables.
Why is this? Is my assumption wrong that when exiting the app all classes and variables will be destroyed?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,事情比这更复杂。
首先,当您在调试器中运行 Android 应用程序时,您将看不到正常的 Android 应用程序生命周期。杀死并重新启动应用程序确实会让您从头开始。不过,正常的 Android 应用程序生命周期对于开发人员来说并不是很直观。通常,如果用户从应用程序“返回”Android 主屏幕,那么一旦没有活动活动,人们就会期望您的应用程序被终止。事实并非如此。 Android 操作系统将让您的应用程序保持活动状态在内存中,直到内存压力导致其释放该应用程序。这样做是因为如果用户再次“打开”应用程序,它的启动速度会快得多。我要指出的是,您的应用程序的 onDestroy() 方法实际上永远不会被终止,因为这也让一些开发人员感到惊讶。
但是,如果您仍然有活动,但它们在后台,并且 Android 需要内存,它会终止您的活动,但会在此之前调用 onSaveInstanceState。这将使您的 Activity 有机会将其状态保存在 Bundle 中,事实上,默认实现会为您完成大部分操作。此时,如果您的所有 Activity 都被终止,您的应用程序也将被终止,但 Android 仍将保持已保存的状态,并且从用户的角度来看,应用程序仍然处于活动状态(它仍会显示在列表中)活动应用程序的数量)。当用户返回到应用程序时,Android将重新构造最顶层的Activity(将调用onCreate,但Bundle包含通过onSaveInstanceState保存的内容)并将其显示给用户。当用户再次将 Activity 从堆栈中弹出时,下面的 Activity 将被重新构造,等等。
Well, it's more complicated than just that.
First of all, you will not see the normal Android application lifecycle when you're running it in the debugger. Killing and restarting the app will indeed start you from the beginning. The normal Android app lifecycle is not very intuitive to developers, though. Normally, if a user 'backs' out of an app to the Android home-screen, one would expect your app to be killed once there are no Activities alive. This is not the case. The Android OS will keep your application alive in memory until memory pressure causes it to release the app. This is done because if the user 'opens' the application again, it will start up much, much faster. I'll point out that your Application's onDestroy() method will actually never be killed, because that surprises some developers too.
If, however, you still have Activities alive, but they are in the background, and the Android needs memory, it will kill your activities, but will call onSaveInstanceState before doing so. This will give your Activity an opportunity to save its state in a Bundle, and in fact, most of this will be done for you by the default implementation. At this point, if all of your Activities are killed, your application will be killed, but the Android will still hold onto the saved state and from the user's point of view, the application is still alive (it will still show up in the list of active applications). When the user returns to the application, the Android will re-construct the top-most Activity (onCreate will be called, but with the Bundle that contains the contents that were saved with onSaveInstanceState) and display it to the user. As the user pops Activities off the stack again, the ones below will be re-constructed, etc, etc.