bob wrote:

> You are not using the singleton pattern properly.\
>
If you're using the Singleton pattern, you aren't using it correctly.
 

> You should access it with a getInstance method which will initialize it if 
> it is null.
>
You should initialize the variable in the class initialization and declare 
it 'final'. 

There are times when you cannot do this, in which case probably Singleton 
is the wrong choice.

For the rest of the times, Singleton probably is the wrong choice.

> 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?
>>
>  
Impossible to tell without seeing your code. You might describe a safe 
scenario 
but really screw the pooch in your implementation.

Give us an example, per 
http://sscce.org/
 
-- 
Lew
 

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
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 android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to