3

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.

14
  • 1
    Did you add the reference to your application class in AndroidManifest.xml? <application ... android:name="com.yourPackageName.application ">
    – Rami
    Jul 22, 2015 at 13:15
  • You will always get value for getApplicationContext(), even without using singleton. What is the problem? Jul 22, 2015 at 13:15
  • In classes that extend no other classes, calling getApplicationContext() is not possible, an unresolved method appears.... @PankajKumar
    – Josh
    Jul 22, 2015 at 13:17
  • Anyhow your classes will be called by any andorid component. So pass context object from there, and make parameterised constructor for such classes, so you can have context object into it. Jul 22, 2015 at 13:22
  • 1
    Consider: stackoverflow.com/questions/4916209/… and for fun also take a look at this comment about subclassing the Application class from Dianne Hackborn (Android frameworks developer): plus.google.com/u/0/+AnderWebbs/posts/DsfpW51Vvow Jul 22, 2015 at 13:44

2 Answers 2

6

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

<application ... android:name="com.yourPackageName.application ">
1
  • Good one, my Application and Singleton classes were correctly implemented, but they did not work until I placed that line on the manifest.
    – Josh
    Jul 22, 2015 at 13:31
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.