Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple classes extending Application

Tags:

java

android

two classes extend Application

  • 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?

A few thoughts:

  • 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: I know I can just use a field to store context in the second class.

like image 742
Dheeraj Bhaskar Avatar asked Jan 21 '13 20:01

Dheeraj Bhaskar


1 Answers

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 :

" 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. "

like image 109
Orabîg Avatar answered Oct 01 '22 22:10

Orabîg