0

I am writing an Android app in which I've extended the Application class in order to hold some global variables. I initialize these global vars in my root Activity (the first screen that the user sees).

My application contains a number of Activities containing ListViews full of selectable items. When the user selects an item, a new Activity is pushed onto the display stack.

Now if a user is in the 3rd or 4th Activity, and then they press the Home button, my app hides. If the user then force closes my app (using a Task Killer), and then reopens my app, the Application context is initialized and the app starts off in the Activity that the user was on when they clicked the Home button. But the problem is, none of my global variables have been initialized at this point since all that happens only on my first Activity.

So is there any way to force my root Activity to always appear when my app comes to the foreground, if the Application object has not yet been initialized?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Wise Shepherd
  • 2,228
  • 4
  • 31
  • 43
  • 1
    Wise Shepherd: Why not "initialize these global vars" in the `Application` itself, in `onCreate()`? – CommonsWare Sep 06 '10 at 20:27
  • Sorry, I should have clarified. These "global vars" are initialized based on the user's input. (The first Activity shown contains several fields for the user to enter some data, with which I initialize a custom Object. Data from this custom object is then used throughout the app and so I want it to be a "global var"). – Wise Shepherd Sep 07 '10 at 16:49

1 Answers1

1

What you can do is check if these global variables are initialized at your activities onResume, and if they aren't, launch the main Activity.

Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
Muzikant
  • 8,070
  • 5
  • 54
  • 88