-2

As the title indicated, I have a doubt that can I save the Application instance(certainly a singleton) as a static field when Application.onCreate() is invoked as the app start, thus I could get the application context anywhere in all of my code even if I don't have a activity, fragment, view or anything else.

That seems to be fantastic but I'm also afraid is there any risks?

Qiang
  • 455
  • 5
  • 15
  • Yes there are potential problems. like inflating layout [Link to similar ask questions](http://stackoverflow.com/questions/987072/using-application-context-everywhere) – rohitanand Feb 04 '17 at 06:19
  • I've browsed that question before ask I also did a demo and found out there is something wrong to do with ApplicationContext used for GUI about. However what I interest in most is to deal with something like SharedPreference with this context, I cannot find a certain YES/NO answer in that question and somewhere else. You know, sometimes a math class also need saved config parameters and it seems to be not elegante to pass a activity context to the math class. @SouravGanguly – Qiang Feb 04 '17 at 06:33

1 Answers1

0

Why don't you get the help from Application class,

Read this > https://developer.android.com/reference/android/app/Application.html

Base class for maintaining global application state. You can provide your own implementation by creating a subclass and specifying the fully-qualified name of this subclass as the "android:name" attribute in your AndroidManifest.xml's tag. The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created.

Note: 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), include Context.getApplicationContext() as a Context argument when invoking your singleton's getInstance() method.

You can do that by keeping a Base class to maintain global application state,here in my example useed the Application object as a singleton object

public class MyApplication extends Application {

    private static MyApplication singleton;

    public static MyApplication getInstance() {
        return singleton;
    }

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

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }



    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

}

In your AndroidManifest specify that using android:name under application tag

<application android:icon="@drawable/icon" android:label="@string/app_name" android:name="MyApplication">

Now you can access this anywhere like

MyApplication mApplication = (MyApplication)getApplicationContext();
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • But since getApplicationContext() is a method of Context and there's a some limitation, will it be OK if I read some config parameters which are saved in SharedPreference via the ApplicationContext gotten by MyApplication.getSingleton.getApplicationContext() ? – Qiang Feb 04 '17 at 06:13