2

I use the singleton approach for a subclass of Application. I use the following:

public class MyApplication extends Application {

    private static MyApplication instance;

    public MyApplication() {
        instance = this;
    }

    public static MyApplication getInstance() {
        if (instance == null) {
            synchronized (MyApplication.class) {
                if (instance == null)
                    new MyApplication();
            }
        }
        return instance;
    }

    ...
    ...

My question is: If the instance is assigned once, during the initial call to the class creator by the system, the instance should never be null after! So if (instance == null) inside getInstance() will never returns true. Or am I wrong?

EDIT:

I correct the code as found on wikipedia:

public class volatile MyApplication extends Application {

    private static MyApplication instance;

    public MyApplication() {

    }

    public static MyApplication getInstance() {
        if (instance == null) {
            synchronized (MyApplication.class) {
                if (instance == null)
                    instance = new MyApplication();
            }
        }
        return instance;
    }

    ...
    ...

Added volatile and instance = new MyApplication(); Is it correct? My question still remains...

Seraphim's
  • 12,559
  • 20
  • 88
  • 129
  • 2
    Your singleton implementation is quite uncommon. Check e.g. the wikipedia article on the design pattern (for example your public c'tor) – Herr K Nov 21 '12 at 19:17
  • I found this example around here. I see the article from wikipedia: http://en.wikipedia.org/wiki/Singleton_pattern, a little different from mine. Is it correct for you? – Seraphim's Nov 21 '12 at 19:24
  • 3
    No, a Singleton depends obviously on a private Constructor. Otherwise everyone just creates a new instance of your MyApplication (besides the fact, that it doesn't make sence at all with a Android Application instance) – Herr K Nov 21 '12 at 19:33
  • but a private constructor raise an error... – Seraphim's Nov 21 '12 at 19:47
  • found an article from IBM about java (does not fit Android): http://www.ibm.com/developerworks/java/library/j-dcl/index.html – Seraphim's Nov 21 '12 at 21:30

3 Answers3

0

It will only be null if you exit the app or Android kills it, in which case, you don't get to check it ;)

You should never create an instance of your application class, so you should not do new MyApplication(). You could extend Application in other classes then instantiate those but you're getting into risky territory and you should question your approach.

See the Application life cycle documentation:

http://developer.android.com/reference/android/app/Application.html

In summary, instance=this is fine for other classes to use ((MyApplication)this.getApplication()).instance;

That said, you should go to Herr K's profile and upvote an answer for him, since his comment is the correct solution, rather than just an answer to your question which mine is.

The point is, you don't need any of that instance stuff. (MyApplication)this.getApplication() will always get you a reference.

Simon
  • 14,407
  • 8
  • 46
  • 61
  • @HerrK Yep. Your comment is on the nail. I've edited my answer. – Simon Nov 21 '12 at 19:32
  • I'm little confused: this.getApplication() can't be called outside activities or services. I use signleton because I need a static reference to the application object from everywhere. Maybe mine is a bad approach... – Seraphim's Nov 21 '12 at 19:47
  • @Seraphim'shost see this discussion http://stackoverflow.com/questions/5018545/getapplication-vs-getapplicationcontext. Does it help? – Simon Nov 21 '12 at 19:53
  • still confused. I mean, I need to access some object I put in the application class from everywhere in the code. So the static getInstance() seems to be the easiest way. – Seraphim's Nov 21 '12 at 20:07
  • @Seraphim'shost So am I :) If you can get to getInstance(), then why not just use the application itself which is, by definition, a singleton. Unless you add a public c'tor, as Herr K says, which you shouldn't. – Simon Nov 21 '12 at 20:17
-1

You are missing one very important thing to getting this to work right.

instance = new MyApplication();

Without this line, then instance will always be null, and you will return null. Otherwise, instance will be created when you call new MyApplication();

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
-1

I use this code for my Singleton class. Try this.

public class MenuPage extends Activity {

    private static MenuPage m_instance = null;

    /**
     * Get the singleton instance of this class
     *
     * @return MenuPage
     */
    public static MenuPage getInstance() {
        if (m_instance == null) {
            m_instance = new MenuPage();
        }
        return m_instance;
    }

    /**
     * Constructor, default
     */
    public MenuPage() {
        m_instance = this;

    }
    /**
     * Called when the activity is first created.
     */
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.main);

    }
}
Zohaib Brohi
  • 576
  • 1
  • 7
  • 15