17

I recently launched my first iPhone app and it seems to have people in the Android community asking for it ... so I started developing w/ the SDK.

The first thing I noticed is that in my iPhone app I would store certain session wide variables in the appDelegate. As I don't have this structure in Android I'm curious how Android developers keep track of application state across the app (hopefully w/out a ton of singleton objects?)

If the singleton object approach is how most developers do this - how can I ensure the application starts at a clean state each time the user clicks the "home" button and re-clicks icon (is there something I can add to my manifest to ensure it doesn't support multitasking in this way?)

My app has a lot of session specific state and for the first iteration won't yet support multitasking :(

JimmyBond
  • 461
  • 2
  • 5
  • 14

3 Answers3

12

First, android app can consist of multiple Activities.

If you want to share state between Activities use Application class: How to declare global variables in Android?

If you only have one Activity, then you can save state there.

Beware: when activity is not Active (it's GUI not showing) it does not mean that it is killed. So if you use your app and then close it, open another app, then go back to you app, it might be still "alive" (kept in memory) and the state would be preserved.

The best way to handle this is to hook into Activity lifecycle. Then you can set/reset data at will.

Community
  • 1
  • 1
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
2

If you want to close the app when the user hits the "home" key, you can call finish() into your onPause method.

See this link: Killing android application on pause

Community
  • 1
  • 1
Chris
  • 328
  • 2
  • 2
  • You can save your variables in a bundle to pass between states. See: http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state – Chris May 06 '11 at 18:46
  • You should not close your app as it removes all possibility of Android managing the limited resources of your device correctly. See http://stackoverflow.com/questions/2439978/why-dont-android-applications-provide-an-exit-option http://android-developers.blogspot.com/2010/04/multitasking-android-way.html http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html - If you're "exiting" your app, you're doing it wrong. – RivieraKid May 06 '11 at 19:52
  • If a developer is going out of his/her way to call finish(), they are doing so for a reason, and I am sure they understand the impact on multitasking. There is nothing inherently wrong with calling it, after all the call is there for a reason as there are times it is needed. Nothing in programming is a catch-all. The worst you will do is slightly increase onCreate time by having to reload the resources again. The Android Dev blog didn't say to never use it, just that the system is designed to not normally require such a call, not that it would never be needed. – Chris May 09 '11 at 15:37
  • While I do agree with you in general, we can (and should) consider Google's statements as best practice and try to find another way. In this case, using the officially sanctioned lifecycle methods (onStop to clear/save and onStart to initialise/restore the session state) are all that's needed. Besides, finishing an activity may not necessarily remove it from memory, and may not necessarily completely clear it's state in all (current or future) Android versions, but properly managing the activity will continue to work. – RivieraKid May 10 '11 at 08:18
1

I find Singletons to be the best way to retain application state in Android applications. You can listen for when the user leaves the application through the onPause() and onStop() methods of the currently focused Activity, and do whatever you want with your data at that point. It's not good practice to try to override the OS's lifecycle management of your application (e.g. trying to kill your process when Back is pressed). If you want the app's state to reset every time the user leaves the application (via pressing Home, or being interrupted with a phone call or notification or what have you), simply put all your session data in the Activity itself. When the user leaves it will be destroyed and recreated when the user returns.

There are obviously specifics that I don't know about your application, but once you get familiar with the lifecycle of each screen (Activity) and of the application, you'll be able to use the callbacks to manage your state however you see fit.

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

LeffelMania
  • 12,765
  • 4
  • 32
  • 34
  • What if application has more than one Activity? – Peter Knego May 06 '11 at 19:03
  • 1
    One note if you decide to store data in the Activity as suggested here: Switching orientation mode or having pop-up interrupts that some apps provide (for example, quick-view SMS apps) will also stop and restart the Activity--which if data is stored only in-memory of the Activity will reset the state. So that probably isn't the solution you want in this case. – Jon Adams May 06 '11 at 19:05