Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9062673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:47:47+00:00 2026-06-16T15:47:47+00:00

Per the Android Documentation it states: There is normally no need to subclass Application.

  • 0

Per the Android Documentation it states:

There is normally no need to subclass Application. In most situation,
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.

How do I go about creating a static singleton that has global context so that it survives the running activity changing in my app? Is it enough to have a static context which references the getApplicationContext()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:47:48+00:00Added an answer on June 16, 2026 at 3:47 pm

    Another edit to the question (2016)

    Lately (as of 2016 and onward) what I’ve been doing, and would be my suggestion for any developer, is:

    Just use Dagger 2. Wherever you need a Context you do:

    @Inject Context context;
    

    and that’s it. While at it, inject all the other stuff that would be a singleton.

    Edited/improved answer (2014)

    because this answer is getting kinda-of popular, I’ll improve my own answer with example code of what I’ve been using lately (as of Jul/2014).

    Start by having the application keeping a reference to itself.

    public class App extends Application {
       private static App instance;
       public static App get() { return instance; }
    
       @Override
       public void onCreate() {
          super.onCreate();
          instance = this;
       }
    }
    

    then on any singleton that needs access to the context I lazy load the singles in a thread safe manner using double check synchronization as explained here https://stackoverflow.com/a/11165926/906362

    private static SingletonDemo instance;
    
    public static SingletonDemo get() {
       if(instance == null) instance = getSync();
       return instance;
    }
    
    private static synchronized SingletonDemo getSync() {
       if(instance == null) instance = new SingletonDemo();
       return instance;
    }
    
    private SingletonDemo(){
       // here you can directly access the Application context calling
       App.get();
    }
    

    Original answer

    what the documentation is suggesting is to use a normal singleton pattern

     public class SingletonDemo {
        private static SingletonDemo instance = null;
    
        private SingletonDemo() {       }
    
        public static SingletonDemo getInstance() {
                if (instance == null) {
                     instance = new SingletonDemo ();
                }
                return instance;
        }
    }
    

    and include inside it a method like this:

     private Context context;
     init(Context context){
        this.context = context.getApplicationContext();
     }
    

    and remember to call this to initialise the singleton.

    The difference between the Application approach and the Singleton approach and why the Singleton is better is on the documentation same functionality in a more modular way

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some things in my Android application that need to update once per
Android application should show Events happening in city as per day/month/year anywhere in the
As per the android developer docs for creating tab UI you need to have
Is this true? Only one BroadcastReceiver class can be specified per application. Should you
Currently I have developed an android application that uses a local sqlite database per
I currently have an application that is storing around 10 variables per android view
I am developing an android application. There is first login page and after that
What is the maximum memory limits per application for Android 2.2?
I've installed the Android Developer tools as per the official instructions . Why doesn't
I have an Android Service that accesses the database about once per minute. Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.