Wednesday, October 10, 2012

Activities, Lifecycle, Callbacks...


One of the easiest but most overlooked concepts in early Android development is understanding the Activity lifecycle. If you don't know what an Activity is, don't worry. Head on over to the developer page to learn more about them. If you don't want to, that's fine too. I'll do my best to explain it here.

Basically, an Activity in Android represents a single screen/page in your app. Every Android application is made up of  at least one, but usually a few Activities. Imagine you have an app that asks the user for their username and password, logs them in, and displays their current bank account information. Let's break down what Activities that app would most likely consist of.

1) The first Activity would be the one that shows the dialog, right? The dialog could also be the Activity itself, but more on that later. Just remember that an Activity represents a single, functional screen. So for now, this is correct.

2) Now let's say that the user has entered their username and password successfully. The dialog box is removed and another screen shows up loading their bank info. This new screen represents a second Activity within the app.

That's it. You're now a master of Android's Activity class, go forth and teach to the masses.

Bicycle? "No, I said Life-cycle"

 Android's Activity lifecycle is a pivotal(note the bold and italics, that means it's impotaahhnt) concept in Android app development. It represents just what the name suggest, the "lifecycle" or different stages of an Android Activity. As we mentioned earlier, Android apps are made up of a bunch of Activities, and that each screen of the app represents a new one.

When someone's using your app, a bunch of different things can happen. What if they received a phone call while playing your game? What if they decided to respond to a text so they closed your app and went back to it after? What if another app on the user's phone is running low on memory? These and so many more things can happen at any given point while your app is being used, and as the developer you HAVE TO(remember what we said about those bold and italics) make sure that you do your best to handle these cases successfully. Trust me.

Obviously you can't account for ALL of the things that could happen while someone is using your app. What if your user is using your app while on the toilet, and accidentally drops it in? Oh well, nothing you can do about that. Hope he has a warranty.

Fortunately, Android's Activity class consists of a bunch of handy methods we can utilize to prevent  and/or handle some of the headaches mentioned above.

Callback Methods

The Activity consists of 7 "callback" methods that you need to know about and when each of them are called, in order to create a functional, user friendly application.

A callback method is basically a method(you should know what these are) that is referenced by another method, being passed in as a parameter. It is called when some specific event occurs, and  it "calls back" or returns a value to it's caller. I suggest you have a look at this Wikipedia article if you're interested in learning more about callbacks. If you are still unsure about this concept. Here's a simple  Java example that I came up with.

//A method that will be called when user clicks a button

private String onButtonClick(){

return "You clicked a button. Hooray";

}

//Method to do something
private void doSomething(String s) {

System.out.println(s);

}
//Start of the program
public static void main(String[] args){

doSomething(onButtonClick());

}

 Continuing with Activities, imagine that the calling class in this case is an activity within your app. Your activity then calls one of these methods when some specific event occurs. This occurs through and through 'til the end of your Activity.

The 7 methods are;

onCreate()
onStart()
onResume()
onPause()
onStop()
onRestart()
onDestroy()

They are easy to identify and because of their names, you get a little insight into what part of your Activity they are responsible for. Each method is invoked when a specific event occurs within your Activity. For example, onPause() handles what happens when your activity is paused and is about to go into the background. I strongly suggest that you take a look at the official Android Developer Page discussing the Activity lifecycle and it's methods.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Web Host