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.

Josh
  • 6,251
  • 2
  • 46
  • 73
  • 1
    Did you add the reference to your application class in `AndroidManifest.xml`? `` – Rami Jul 22 '15 at 13:15
  • You will always get value for `getApplicationContext()`, even without using singleton. What is the problem? – Pankaj Kumar Jul 22 '15 at 13:15
  • In classes that extend no other classes, calling getApplicationContext() is not possible, an unresolved method appears.... @PankajKumar – Josh Jul 22 '15 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. – Pankaj Kumar Jul 22 '15 at 13:22
  • I want to encapsulate commonly used methods such as singleton.toast("hello") without worrying about context, however, singleton will do that work for me. It is cleaner and more maintainable than lobbing context from every class. – Josh Jul 22 '15 at 13:24
  • Avoid subclassing the Application class if you can. It's preferable to init your singletons from your Activity (or lazily when needed). Also consider if you really need a singleton at all, needing a singleton usually shows a design issue in your app – JohanShogun Jul 22 '15 at 13:24
  • Thanks @Rami ! That was the missing piece in the puzzle. Put it as an answer and I will give you the green tick. – Josh Jul 22 '15 at 13:25
  • I have heard that Singletons are evil, but my skill level on android won't let me see a better solution. Where would you put all those methods that are called frequently from every sort of class for example (from activities, from rows inside a list, from adapters, from fragments, etc.)? @JohanShogun – Josh Jul 22 '15 at 13:30
  • If you have that sort of thing it seems to me that you aren't holding strict responsibilities for your classes and that you're mixing GUI code with application logic code. There are circumstances where Singletons are the best solution, but then it is usually to solve a specific problem. – JohanShogun Jul 22 '15 at 13:38
  • In fact GUI and application code are quite separated. My service classes provide specific functionalities to several classes.... even to other apps by using broadcasts. What alternative would you recommend instead of a "singleton.toast("hello")" method than can be called from any class (because it encapsulates its own appContext) in the app (not only activities/fragments, but buttons inside dynamic list rows, services, broadcast receivers) ??? @JohanShogun – Josh Jul 22 '15 at 13:43
  • 1
    Consider: http://stackoverflow.com/questions/4916209/which-design-patterns-are-used-on-android/6770903#6770903 and for fun also take a look at this comment about subclassing the Application class from Dianne Hackborn (Android frameworks developer): https://plus.google.com/u/0/+AnderWebbs/posts/DsfpW51Vvow – JohanShogun Jul 22 '15 at 13:44
  • Thanks, I will read it thoroughly, even if my skills are not top notch I make an effort to keep an open mind always. @JohanShogun – Josh Jul 22 '15 at 13:46
  • I don't see a need to wrap toast functionality in a singleton. If you are in a context where updating the GUI is prudent then you already have access to an ApplicationContext or in the case of button sub classes you've been given the chance to make a local reference to the application context already. Consider creating callbacks back to classes responsible for GUI manipulation instead of arbitrarily updating your GUI from many different places :) – JohanShogun Jul 22 '15 at 13:48
  • Maybe next time, my app runs smooth like a swiss watch now. @JohanShogun – Josh Jul 22 '15 at 13:53

2 Answers2

6

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

<application ... android:name="com.yourPackageName.application ">
Rami
  • 7,879
  • 12
  • 36
  • 66
  • 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 '15 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.

anupam_kamble
  • 144
  • 2
  • 6