Blog About Android Development

Android Development

Android Development

Sep29

{Android Activity Classes Explained in Detail}

As a fledgling Android developer one of the first things you’ll need to do is get your head around Activity Classes. We can’t overstate the importance of this. A sharp, fluid understanding of how each class interacts with each other class, and the end result for end users will not only speed along your project dev time, but open up new possibilities for your programming. So let’s take a look at these in some detail.

  • onCreate(): This is called when the activity first starts up. You can use it to perform one-time initialization such as launching the user interface. onCreate() utilizes one parameter that is either null or state information previously saved by the onSaveInstanceState( ) method, discussed below.
  • onStart(): This indicates the activity is about to be displayed to the user.
  • onResume(): This is called when the user can start interacting with the activity. This is a good place to start animations and music.
  • onRestart(): If this is called, it indicates your activity is being redisplayed to the user from a stopped state.
  • onFreeze(): Allows you to save your current state when one activity is being paused and another one resumes to interact with the user. After being paused, the system may at any time need to stop (or even outright kill) your application in order to claim resources for the current foreground activity. If this should happen, the state you supply here will later be recalled with onCreate(), when the user starts a new instance of your activity.
  • onPause(): This runs when the activity is about to go into the background, usually because another activity has been launched in front of it. This is where you should save your program’s persistent state, such as a database record being edited.
  • onStop(): This is called when your activity is no longer visible to the user and it won’t be needed for a while. If memory is tight, onStop() may never be called (the system may simply terminate your process).
  • onDestroy(): This is called right before your activity is destroyed. If memory is tight, onDestroy( ) may never be called (the system may simply terminate your process).
  • onSaveInstanceState(Bundle): Android calls this method to allow the activity to save per-instance states, such as a cursor position within a text field. Usually you won’t need to override it because the default implementation saves the state for user interface controls automatically.
  • onRestoreInstanceState(Bundle): This is called when the activity is being reinitialized from a state previously saved by the onSaveInstanceState() method. The default implementation restores the state of your user interface.

I hope this brings you up to speed on Android Activity Classes. Be sure to have a thorough look at the attached diagram below (click –> full size) for a concrete overview of what we’ve been discussing here. The more familiar you are with Activity Classes and how and they affect the end user experience, the more successful your Android apps development will be.

If you have any questions or requests re: Android apps dev, drop us a line via the contact form at mothership Sourcebits and we’ll get back to you as soon as we can, perhaps even posting a tutorial here on these pages.

Best of luck,
Sudhir N Kamath
Sr. QA Engineer
Sourcebits Technologies, Ltd.

Learn it, Live it, Reap ze glory.

Posted by admin in Development | Tutorials Comments: 0 Add comment

Comments

No comments yet.

Add Comment

You must be logged in to post a comment.