扩展应用程序的多个类 [英] Multiple classes extending Application

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

问题描述

  • 在我注册的清单,并正在使用为应用一类的意思是
  • 第二类是我的实用工具类。它做大量的I / O,并有一些辅助方法。对于I / O需要上下文(getAssets等),所以我不情愿地扩展应用程序。
  • One class I've registered in the Manifest and am using as an Application is meant to be
  • Second class is my utilities class. It does lots of I/O and has a few helper methods. For I/O you need context (getAssets etc), so I reluctantly extended Application.

注意:

Note:

一切工作,因为它应该。

Everything is working as it should.

我的问题:

My Question:

是否有使用多个应用程序类的什么缺点?这是甚至建议?

Are there any drawbacks of using multiple Application classes? Is this even advised?

的几点思考:

  • 一样,如果我的onCreate等回调方法在两个类中定义会发生什么?
  • 如何在清单中,甚至它们注册两者兼而有之? 等
  • Like what would happen if I had onCreate and other callback methods defined in both the classes?
  • How do I register them both in the manifest even? etc

PS:我知道我可以只使用一个字段来存储环境中的第二类

PS: I know I can just use a field to store context in the second class.

推荐答案

我觉得这是不建议在所有的,因为只能是对应用程序(因此只有一个班)。

I think this is not advised at all, because there is could only be one instance on Application (thus only one class).

我很怀疑什么是真正的工作。你说的是工具类,所以也许你使用的是运作良好的静态方法。但是,你应该用你的调试器,我几乎可以肯定,你会发现你的类之一从不实例。

I am very suspicious about what is really working. You're talking about utility class, so maybe you're using static methods that are working well. But you should use your debugger, and I'm almost certain that you'll discover that one of your classes is never instantiated.

顺便说一句,官方文档指出:

By the way, the official documentation states that :

通常没有需要继承的应用。在大多数情况下,静态的单身人士可以提供一个更加模块化的方式相同的功能。如果你的单身需要一个全球范围内(例如注册广播接收器),该函数检索它可以给出一个Context,它内部使用Context.getApplicationContext()时,首先构建了单。

" There is normally no need to subclass Application. In most situations, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton. "

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

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