Josh
Josh

Reputation: 6383

How to initialize an Application class and store getApplicationContext() in android?

I have declared an Application class inside my app:

public class application extends Application {

    String TAG="joshtag";//app.TAG;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "Initializing APP");
        initializeSingletons(getApplicationContext());
    }

    protected void initializeSingletons(Context c){
        Log.e(TAG, "...preparing to Initialize Singleton");
        app.initInstance(c);
    }
}

As you may imagine, I want this class to run as soon as my App launches, with the objective of initializing a Singleton class (this singleton class does not extend any other class and will contain a variable to store getAppContext). However, the Log messages never trigger inside "onCreate" and "initializeSingleton", and the Singleton is not initialized. My ultimate goal is to store a global getAppContext variable that I can use inside classes who does not inherit it. If I am approaching the issue with the wrong mindset, let me know and mention why.

Upvotes: 3

Views: 6257

Answers (2)

anupam_kamble
anupam_kamble

Reputation: 144

Was facing the same problem. To initialise an Application class we need to specify it in manifest file with corresponding package name inside application tag.

Upvotes: 3

Rami
Rami

Reputation: 7929

Don't forget to add the reference to your application class in AndroidManifest.xml.

<application ... android:name="com.yourPackageName.application ">

Upvotes: 6

Related Questions