5

I am using Application class to share global variables across activites and I am setting them in onCreate method of application class. When I start app variables values are set in onCreate and while using app in activities I am changing values of varables. When I exit app and start it again I am getting old values, the last values of variables set in activities. Thats mean onCreate of Application not running on starting app again. This is code in onCreate method of Application class.

@Override
    public void onCreate() {
        super.onCreate();
        application = this;
        category = 12;
        subCategory =22;
    }

It looks like old application object is still in memory and it is not calling onCreate on starting app 2nd time.

What is need to be done so that onCreate of application class run again or where to initialize variables in application class so that code runs everytime.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
anujprashar
  • 6,263
  • 7
  • 52
  • 86

6 Answers6

7

please declare your application class name in manifest file. like below

<application
    android:name="com.tt.app.TTApplication"
    android:label="@string/app_name"
Narender Gusain
  • 2,220
  • 17
  • 13
1

In the Application class, the onCreate() method is called only if the process was ended when you exited the application. Usually the process is stopped when the system needs memory or if you exit the app using the back button instead of the home button. However, you cannot rely on it being terminated.

However, the right way of passing parameters between activities are intents or preferences. In your case, I have the feeling that preferences is the way to go.

If you really want to kill your process when exiting the application, you can call System.exit(0); when the user presses the back key on your first activity. This is definitely not recommended since it means fighting against the way the Android OS works and might cause problems.

More on this here: Is quitting an application frowned upon?

Community
  • 1
  • 1
Daniel
  • 984
  • 8
  • 14
  • Thanks for reply I am setting variable value in onCreate of Application class and I just checked there is no onResume in Application class. – anujprashar Jan 23 '12 at 09:51
  • I guess everybody thought you are using an Activity. In order to share values across activities I would not use variables. I suggest using `SharedPreferences`. Check out this link: http://developer.android.com/guide/topics/data/data-storage.html#pref – Daniel Jan 23 '12 at 10:35
  • 1
    You can set the preferences values in one activity (or application) class, and then you can read them everywhere across your application. The advantage is the low coupling between classes, and also the persistence, which lasts also after you leave the application, since preferences are xml files stored in your application sandbox directory. (/data/data/your.package.name/) – Daniel Jan 23 '12 at 10:37
  • Thanks for replying. I am talking about Application class not activity. Atleast you got it right. So you think it is better to share variables using SharedPreferences instead of using Application class. I will look into it. – anujprashar Jan 23 '12 at 12:13
  • I also edited my answer. If it helped, please mark it as the solution. Thanks. – Daniel Jan 23 '12 at 13:33
0

There is probably an instance of your application still in the memory.

Recheck your life cycle methods and make sure that the application is exiting properly.

Also check if any of your activities are leaking memory.

Hades
  • 3,916
  • 3
  • 34
  • 74
-1

I had the same problem with my app where onCreate() method of Application class just triggered for the first time when my app is loaded. Daniel's solution of using System.exit(0) did the trick but this solution lead me to another problem. After using System.exit(0), onPause(), onStop() and onDestroy() method of my foreground activity did not get called.

Well, that was a reasonable behavior for an app because If you use System.exit(0) then you application will be removed from System's process queue and there will be no way for an android to execute onPause(), onStop() and onDestroy() method for my foreground activity.

The workaround I used for this problem was to finish my activity when back button is pressed and after some time killing my applications process like below:

public void killApp(){
    final Thread threadKillApp = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.i(TAG, "Going to kill app");
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    });
    threadKillApp.start();
}

Calling killApp() method just after calling finish() on my activity did the job.

Vikalp
  • 2,051
  • 4
  • 18
  • 24
-3

try to use onStart() method or onResume().

Your onCreate method should look like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(someView);
}

your onResume Method should look like this:

@Override
public void onResume() {
    super.onResume();
    variable = someVariable;
}
Dawid Sajdak
  • 3,064
  • 2
  • 23
  • 37
  • 2
    Thanks for reply I am setting variable value in onCreate of Application class and I just checked there is no onResume or onStart in Application class. – anujprashar Jan 23 '12 at 09:50
-3

Check the Activity life cycle. Do what you want in onResume() instead.

Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47
  • Thanks for reply I am setting variable value in onCreate of Application class and I just checked there is no onResume in Application class. – anujprashar Jan 23 '12 at 09:50
  • onResume() is part of the life cycle of the Activity class (please check the link). you override this method to do what you want. – Mohamed_AbdAllah Jan 23 '12 at 10:25