简体   繁体   English

Android:拥有永远不会被杀死的Singleton类

[英]Android: have Singleton class that will never been killed

In my Android application I have a Singleton class. 在我的Android应用程序中,我有一个Singleton类。 It works like a charm when the application task is alive. 当应用程序任务处于活动状态时,它就像是一种魅力。 But after the application task is being killed, the Singleton class gets destroyed, which is obviously. 但是在终止应用程序任务之后,Singleton类被销毁,这很明显。 I need to keep this class always alive. 我需要让这堂课永远活着。 Is it possible? 可能吗? And what is the best approach to have this class never been killed by Android system, do I need to use a background Service in this situation? 避免此类被Android系统杀死的最佳方法是什么?在这种情况下,我需要使用后台服务吗?

Using a Service for the purpose of trying to ensure a Singleton class always stays in memory for subsequent invocations of the application would be terribly bad design. 为了试图确保Singleton类始终保留在内存中以便后续调用应用程序而使用Service的设计是非常糟糕的。 It's simply not what Services are for. 这根本不是服务的目的。 See: http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/ . 参见: http : //www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/

If you want to ensure that a bunch of data is kept around ready for the next time the application is created then you should choose an appropriate means to persist that data. 如果要确保为下次创建应用程序准备好一堆数据,则应选择一种适当的方法来保留该数据。 This could be, for example, by using SharedPreferences or SQL , or a cache, etc. depending on what kind of data you're trying to keep around. 例如,这可以通过使用SharedPreferencesSQL或缓存等来实现,具体取决于您要保留的数据类型。 Then, design all Activity classes to properly cope with the scenario where they're resumed when all Singleton data is not in an initialised state. 然后,设计所有Activity类,以正确处理所有Singleton数据均未处于初始化状态时恢复它们的情况。

I personally maintain a complex scientific application that relies on having quite a lot of singleton data held in RAM. 我个人维护着一个复杂的科学应用程序,该应用程序依赖于RAM中保存大量单例数据。 One significant problem I encountered during early stages of development is that it's possible for the following situation to happen: 我在开发的早期阶段遇到的一个重要问题是,可能发生以下情况:

  • You have three Activity classes (for example); 您有三个Activity类(例如); call them A, B and C. 称它们为A,B和C。
  • You have Singleton data attached to the Application class. 您已将Singleton数据附加到Application类。
  • User makes use of the application and then navigates to Application C. By this time, lots of Singleton data has been initialised. 用户使用该应用程序,然后导航到应用程序C。这时,已经初始化了许多Singleton数据。 Then, the user navigates back to home (the launcher). 然后,用户导航回到家(启动器)。
  • In the meantime, your app's Application class is destroyed and created (perhaps several times over) along with any Service s it uses while the user interacts with other apps. 同时,您的应用程序的Application类以及它在用户与其他应用程序交互时使用的任何Service被销毁和创建(可能要重复几次)。
  • Later, the user goes back to your app. 稍后,用户返回到您的应用程序。 The Activity stack is resumed, meaning that the user goes straight to Activity C. However, the Singleton data is now all gone. 恢复了活动堆栈,这意味着用户直接进入了活动C。但是,单例数据现在已全部消失。 Having Activity C start up with an "empty" non-initialised set of data might have resulted in a crash or the user being presented with a blank page. 使用活动的“空”未初始化数据集启动C可能会导致崩溃或向用户显示空白页。
  • Key point: It's possible for the Activity stack to be resumed so that the app is resumed back on Activity C even though the Application class has been restarted and all global data has been nuked from orbit. 关键点:有可能恢复Activity堆栈,以便即使Application类已重新启动并且所有全局数据已从轨道移走,应用也可恢复到Activity C上。

To cope with the above scenario, the key thing I did was to ensure all Activities don't just assume that global (Singleton) data is present and intact when the Activity is started. 为了应对上述情况,我要做的关键是确保所有Activity都不会仅仅假设在Activity启动时就存在完整的全局(Singleton)数据。 Each Activity in my application that depends on the global Singleton data checks that data first and, if it is not initialised, calls a method in my Application class to trigger regeneration of that data (which in my case involves retrieving that data from a file from the SD card). 我的应用程序中依赖于全局Singleton数据的每个Activity首先检查该数据,如果未初始化,则调用Application类中的方法以触发该数据的重新生成(在我的情况下,这涉及从文件中检索该数据。 SD卡)。

So in short, handling global / Singleton data in Android takes some care and thought. 简而言之,在Android中处理全局/单例数据需要一些注意和考虑。 You cannot force the system to indefinitely avoid killing your Application and therefore all of your Singleton data. 您不能强迫系统无限期地避免杀死您的应用程序,从而杀死所有您的Singleton数据。 The key things I learned therefore are to persist and retrieve your data, and design your Activity classes to properly handle the situation where no Singleton data is found to be already initialised when that Activity is created. 因此,我学到的关键知识是持久性和检索数据,并设计Activity类以正确处理在创建Activity时未发现尚未初始化Singleton数据的情况。

是的,您需要使用后台服务。后台服务是与其他所有组件相比在被杀死方面具有最高优先级的组件,但是请确保或服务应连续运行。

Try using moveTaskToBack() . 尝试使用moveTaskToBack() That should do the trick. 这应该够了吧。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM