Android应用程序的生命周期类 [英] Android Application Class Lifecycle

查看:126
本文介绍了Android应用程序的生命周期类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android应用我的工作覆盖应用程序类存储静态瓦尔轻量级的状态(用户名,GPS定位等)。大部分这种状态被设置在发射活动(用户名由preFS检索,位置监听器运行)的OnCreate中。它是安全的依靠就推出活动,以初始化应用程序类?有没有那里的应用程序类可能也正在创建重新创建没有启动活动的任何情况下?

The android app I am working on overrides the Application class to store lightweight state (username, gps location, etc) in static vars. Most of this state is set in OnCreate of the launch activity (username retrieved from prefs, location listener runs). Is it safe to rely on the launch activity to initialize the Application class? Are there any cases where the Application class might be re-created without the Launch activity also being created?

现在的问题来了,因为我遇到了一个空指针异常的恢复应用程序后,手机就睡着了数小时(应用程序被留在前台之前,手机睡觉去了)访问应用程序类的变量。难道这个过程被打死,而手机是睡着了,醒来的手机,应用程序类是重新创建,在栈上的活动得到恢复,但推出activity.onCreate不是这样运行的应用程序类未初始化?

The question comes up because I ran into a null pointer exception accessing a variable in the Application class on resuming the app after the phone was asleep for several hours (the app was left in the foreground before phone went to sleep). Is it possible that the process was killed while the phone was asleep and on waking the phone, the Application class was re-created, the top activity in the stack was resumed, but the launch activity.onCreate wasn't run thus the Application class wasn't initialized?

请注意,我试图通过迫使应用程序来测试这些类型的方案,停止使用设置/管理应用程序。不过,我不能够重现问题。在接下来的运行中,应用类被创建,接着发射activity.onCreate

Note that I have tried to test these kinds of scenarios by Forcing the App to stop using Settings / Manage applications. However, I'm not able to recreate the problem. On the next run, the Application class is created, followed by the launch activity.onCreate.

它是安全的假设,应用类的实例会一直存在的过程中,已创建应用程序类时,它等同于重新启动的应用程序,即。有一个新的活动栈开始(和堆栈的第一项活动是发射活动)?

推荐答案

没有。整个应用程序可以被杀死,并重新创建任务堆栈完好;这使系统回收内存在需要它,同时仍然presenting多任务给最终用户的无缝错觉设备。从文档:

No. Your entire application can be killed and recreated with the task stack intact; this lets the system reclaim memory on devices that need it while still presenting a seamless illusion of multitasking to the end user. From the docs:

一个背景活动(活动   是不可见的用户和   已暂停)不再   关键的,因此该系统可安全地   杀死它的进程回收内存   其他前景或可见的过程。   如果其进程需要被杀害,   当用户导航回   活性(使其在可见   屏幕再次),它的onCreate(捆绑)   方法将被调用的   savedInstanceState它有previously   在供应   的onSaveInstanceState(束),以便它   可以在相同的状态重新启动本身   作为用户最后离开它

A background activity (an activity that is not visible to the user and has been paused) is no longer critical, so the system may safely kill its process to reclaim memory for other foreground or visible processes. If its process needs to be killed, when the user navigates back to the activity (making it visible on the screen again), its onCreate(Bundle) method will be called with the savedInstanceState it had previously supplied in onSaveInstanceState(Bundle) so that it can restart itself in the same state as the user last left it.

也就是说,这个过程(该应用程序绑定到)可以被杀死关闭,但随后重新启动,和个人的活动应该有足够的信息,从他们被杀害之前已经保存了什么意趣盎然,而不依赖于全局状态其他活动的过程中设置的。

That is, the process (which the Application is tied to) can be killed off but then restarted, and the individual activities should have enough info to recreate themselves from what they've saved before being killed, without relying on global state set in the process by other Activities.

考虑将需要初始化由活动在任何一个共享preference或SQLite数据库,或者将它传递给需要它作为一个意向课外活动永久共享状态。

Consider storing persistent shared state that needs initialization by an Activity in either a SharedPreference or SQLite database, or passing it to Activities that need it as an Intent extra.

这篇关于Android应用程序的生命周期类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆