0

This is a problem that is puzzling me and I wanted to get answers.

We have extended the Application and the class has variables that are to be accessed from different areas of the app. I think this was done to get a singleton implementation. The application has member objects of different classes.

public class DataApplication extends Application{
public InfoHelper mInfoHelper;

public void setHelper (InfoHelper infoHelper) {
  mInfoHelper = infoHelper;
 }

public InfoHelper getInfoHelper() {
        if (mInfoHelper == null){
            mInfoHelper = new InfoHelper();
        } 
        return mInfoHelper;

}

// the InfoHelper class

public class InfoHelper{
   public int trial = 10;
}

Now in an AsyncTask (which is started from an Activity A) updates the value of trial variable like this,

mDataApplication = (DataApplication) ((FragmentActivity)context).getApplication();

InfoHelper infoHelper = mDataApplication.getInfoHelper();

infoHelper.trial = 200;

when the AsyncTask is done, inside the Activity-A, I check the value of the trial variable using and InfoHelper variable that was created before calling the AsycTask,

// The below is created in onCreate() of Activiy-A, before the AsyncTask is started.

mInfoHelper = mScanApplication.getInfoHelper();

// After the Asyctask is done I would check the value of trial.

Log.d(TAG, "Test Value 1 --- "+ mInfoHelper.trial);
Log.d(TAG, "Test Value 2--- "+ mDataApplication.getInfoHelper().trial);

the result is, Test Value 1 --- 10 Test Value 2--- 200

I was expecting the first result from the first Log statement but I did not expect the second log statement to get me the updated result. How did that happen? When updating in the AsyncTask, I created an object of InfoHelper and updated that object. How come it reflected everywhere. My friends said that it was because it was the same reference of the object and it does not matter if we create an object of the InfoHelper as it is the same memory location, if that is true then the question would be why the first Log statement showed me the old value?

My fundamentals are weak in this area and I appreciate may help in my quest to become better.

Note: If I create the mInfoHelper object again right before printing it I have the updated value which I was expecting to happen.

mInfoHelper = mScanApplication.getInfoHelper();
Log.d(TAG, "Test Value 1 --- "+ mInfoHelper.trial);

the result is, Test Value 1 --- 200

luckylukein
  • 819
  • 1
  • 7
  • 17
  • You're never setting "infoHelper" in your "getInfoHelper" method. You're just returned a new one every time it's null (which is always unless you call "setInfoHelper". – DeeV Aug 04 '16 at 01:59
  • I agree with this comment, what you are doing is initializing the object in a method which aims to return the object - to follow a true singletone suit, you should initialize in the Application's onCreate. I can refer you to a github example of mine - I use the approach here when making application a singleton in every app. https://github.com/apelsoczi/NFCAlarm/blob/master/app/src/main/java/com/example/adam/nfcalarm/MyApplication.java – apelsoczi Aug 04 '16 at 02:06
  • @DeeV - Why would a new one be returned every time? The very first time I call getInfoHelper(), a new one would be created and set as the member variable. The second time it does not go through the new call instead returns the member variable. Yes, I could have used setHelper() and it is used from some other places but that is besides the point. Wouldn't getInfoHelper() work fine? – luckylukein Aug 04 '16 at 02:18
  • 1
    Sorry I think your method is incomplete. It's not even returning anything if the info helper is null. – DeeV Aug 04 '16 at 02:21
  • @apelsoczi - I do not want to achieve true singleton as in some scenarios I have to set the infoHelper from outside and that is why the setHelper() method. All I want to understand is the working of the code not make a singleton suit. Thanks for the note though. Appreciate it! – luckylukein Aug 04 '16 at 02:23
  • @DeeV - Ahh Yes, Sorry I had to edit and make changes to the code as I did not want to post the original solution.. The Else was not required. My bad! Thanks for pointing out the mistake. I have made the edit. – luckylukein Aug 04 '16 at 02:29

0 Answers0