14.06.2011

Android app life cycle

posted by Karsten

An Android application has a slightly different life cycle than a traditional java application. First of all there are no main() to start of things, the Android environment is in control, and introduces a few different “states” that an app can be in.

 

The first time an app is started the onCreate() method is called. This method will only be called, however, when the app is started up from scratch. So for instance if the user goes away from the app (e.g. to take a phone call or check something on the web), then Android calls the onSaveInstanceState() method of the activity, where the user can save the state of the app into a bundle, and Android puts the app into the background. Depending on factors outside of the app developers reach, the app now either stays in the background or is destroyed (i.e. to free up resources or the phone is closed). The app can then be either restarted or recreated through user interaction. The app developer needs to ensure that the app responds to all of these possibilities. Things to remember:

  • Implement onSaveInstanceState()
  • Implement specificly what to do if Bundle savedInstanceState in onCreate isn’t null (i.e. the game is a previously saved game)
  • Make sure that appropriate resources are stopped and destroyed at the different levels of the app life cycle. Remember resources that are referenced by “live” object instances cannot be destroyed by the garbage collector, and could lead to memory leaks.
  • Make sure that the app can perform a restart from being stopped.
Share

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment