2

In Android we often have to use Context classes. When a class or method requires a Context we often use Activites or Services for such arguments. The following code is from a website I found but I do not know if this is good practice and if it is okay to use this solution:

public class MyApplication extends Application {
    private static MyApplication singleton;

    @Override
    public void onCreate() {
        super.onCreate();
        singleton = this;
    }

    public static MyApplication getInstance() {
        return singleton;
    }
}

In first place it looks okay to me to use a singleton pattern here. I mean every time some of my app code is executed in Android the system creates a process and therefore also an application context exists which I can use in other classes.

On the other hand it fells wrong to use this. With this pattern every class (also pojo and singletons where we should avoid a Context object) are able to simply get a valid reference to the actual Context which (I think) is not the idea behind the Context object.

So what do you think about this solution? Is it okay to use it or are there some reasons (e.g. lifecycle of application, etc.) to avoid this? Or are some assumptions from me here wrong?

Vik David
  • 3,640
  • 4
  • 21
  • 29
Cilenco
  • 6,951
  • 17
  • 72
  • 152
  • 3
    https://stackoverflow.com/a/21336200/115145 – CommonsWare Jul 30 '17 at 16:53
  • This is not a singleton. In order to have a singleton you have to declare a `private` constructor. What the above code is doing is to save the instance of your class in a `static` attribute. – anemomylos Jul 30 '17 at 17:32
  • @CommonsWare Thank you for the link. I search a bit around but apparently haven't found the right keyword for this question, sorry. – Cilenco Jul 30 '17 at 17:38

2 Answers2

1

Yes, you are correct. It is possible to design the application in such a way that we don't need this Application class Instance.

I use Dependency Injection pattern in my project. Say, for instance, my Presenter class needs a DataRepository class instance to fetch the data. And the DataRepository class needs a Context to access Database or SharedPreferences.

It is possible to create the DataRepository class inside my Presenter class using the Application instance. But using Dependency Injection pattern, I create the DataRepository outside of the Presenter (possibly Fragment/ Activity) and the pass the DataRepository instance to the Presenter via its constructor.

Bob
  • 13,447
  • 7
  • 35
  • 45
0

Very good @CommonsWare's answer... just complementing...

I believe it is a bad solution because it can cause a Memory Leak... Even though it is very very difficult to happen it... to use a static reference to a Context instance is very bad because this Application instance will not be cleaned when ActivityManagerService destroys it due to that static reference... -- As commented below, it cannot cause any memory leak in this case.

I don't like this kind of solution... it's safer to use the Context directly than it (e.g. getApplicationContext()).

obs.: It is also Violating Singleton Pattern because it is not restricting the instantiation of the class and doesn't ensure that there can be only one instance of it... but it is not relevant...

Victor Rattis
  • 534
  • 5
  • 10
  • 1
    what is the meaning of violating when application class tied to whole app jvm! there is no leak. the answer is wrong. but of course using DI is better solution – Siavash Abdoli Oct 15 '17 at 09:15
  • 1
    This cannot cause a memory leak. The application is already a singleton within the process; you're just creating a second reference to it. When the Android system needs to reclaim resources, it doesn't garbage-collect the application; it simply kills off the process itself. There is no `onDestroy()` method for the `Application` class because application objects (unlike application component objects) aren't destroyed by `ActivityManagerService`. The real problem with OP's posted code is that it encourages the use of the application context when it is not appropriate. – Ted Hopp Jul 20 '18 at 19:00
  • Yes, you are right about that, I have analyzed again and I see that cannot cause a memory leak, Thanks for your comments. When I said that, I thought just existed only one application instance by the package... and the right way it is to have an application by one process... and the application instance is killed together with its process. – Victor Rattis Feb 26 '19 at 18:04