Android: Singleton vs ApplicationContext

Well here’s a topic that’s hotly debated in Android design.

Technically, both of them could work for rapid development. However, I prefer Method B as you can modularize your singletons to support specific functions in each class definition.
Best if you can work without them, but its often not the case.

Method A – Extending Application Context
Step 1: Create a class that extends Application.

Step 2: Specify in the AndroidManifest.xml

Step 3: You can now access the application context anywhere using

Method B – Using static singletons (Recommended)

Step 1: Create a class

Step 2: You can now access the singleton anywhere using

  2 comments for “Android: Singleton vs ApplicationContext

  1. January 8, 2016 at 3:20 am

    In method 1 your class is Globals and the “access anywhere” is GlobalAccess.get();
    Is this a typo. or did I miss something?

    I’ve used this and named mine DataModel, but I also used that name in my manifest.
    android:name=”DataModel”>
    Should I be using .?

    However mine stopped working when I switched to Fragments even though I use getActivity() to access the application

    • February 23, 2016 at 3:27 pm

      Thanks, yes it was a typo Globals.get(), not GlobalAccess.get(). I’ve fixed that.

      If DataModel.java is not under your main package, then you’ll need to specify it with your full package details. Eg. com.trevor.somepackage.DataModel

Leave a Reply

Your email address will not be published. Required fields are marked *