11

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.

2
  • 1
    The thing that's missing is why did you extend Application, rather than just create your own static singleton to implement your helper methods? If you need context, then just use getApplicationContext, or implement something like setHelperContext(). If you only have one instance of application, this is perfectly safe since nothing will have a life time greater than the application context. It only becomes problematic if you pass Activity contexts around (risking leaks) but a helper class should not be touching Activity resources anyway. Commented Jan 21, 2013 at 21:31
  • Why, Sir? I ask why not?. Ok seriously :), I just thought it would work and it kinda did. I needed context and a few other things, didn't want to have fields for everything. As you said, I could've created a singleton and passed in context. I'm not even touching activity contexts here, I'm using getApplicationContext. Helper class is helping with I/O, so it is manipulating Resources. Commented Jan 22, 2013 at 1:31

1 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

8 Comments

Yes, I am using static methods in Utility class. What is working is that the IO methods which require Context are working fine(how is context managed here if class is not instantiated? .. interesting)
I'm curious. Multiple instances of activities in different tasks aren't considered multiple instances of an application?
Yes, but if they are static method, then you must pass the Context in the methods parameters ? So any class would work (and you should inherit your class from Object). It would be problematic if you had replaced "context" by "this" (which could work only on the real and single Application instance)
Answer to question #2 : No. Application and Activities are very different classes. There could be many activities by application of course (usually one for each "screen")
that much I understand that Activity and Application are different classes. but the contexts and global variables in these two tasks, say, would be very different right. That would mean two instances of the application.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.