You are not using the singleton pattern properly.


You should access it with a getInstance method which will initialize it if 
it is null.



On Monday, February 25, 2013 3:52:30 AM UTC-6, user123 wrote:
>
> I have an instance variable in a singleton class which I'm using in all 
> the app. It's basically a global configuration parameter.
>
> I initialize it in the first (launcher) screen of the app. The app will 
> not continue without this initialization (where it's sure that the variable 
> is not null). And I'll not use the variable before of this.
>
> After the variable is initialized, the value is never set again, only read.
>
>
> And I'm wondering, if there's any case where I can get a null pointer? 
> Because e.g. the system kills the app while it's in the background, and 
> tries to restart it in the last screen - since the variable is only 
> initialized in the launcher screen, it will not be initialized? Does this 
> case exist?
>
> I made a few tests - I stopped the app from settings. Then the app started 
> from launcher screen, so everything fine. I also threw a RuntimeException 
> from a random screen. After it, the system started the app from launcher 
> screen, so again, fine.
>
> But I don't know if background app killed by the system behaves 
> differently. Can't simulate that.
>
> And maybe there are other cases which I'm missing.
>
> So, is there any situation where my variable can become null? Or is this 
> setup safe?
>
> Thanks in advance.
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to