Activity class in android application development

Activity class in android application development

In previous article  we explored components of android application development.  In this lecture we will discover activity class in detail. Activity class are the primary class that  through which the user can interact with the application.

Each activity should support a single focused thing a user can do  like viewing an email or viewing a login screen. So by following this convention we end up by creating an application that consists of multiple activities each with a single purpose that user can navigate through.

Android supports navigation through several ways such as

  • Tasks
  • Tasks Back Stack
  • Ensuring that activities are properly suspended and resumed as they are push on and poped off  of tasks backstack

What is a Task?

A task is a set of related activities. These related activities don’t have to be part of  same application.  This means that a task can span to multiple applications. when a user launches an application from home screen a task is started.

What is Task Back Stack?

when ever an activity is launched, it goes on the top of a task back stack, normally as part of current task. when ever an activity is destroyed due to any reason like user pressed home button or back button the activity is poped off the back stack.

android task backstack

When activity 1 is started it is pushed to the top of back stack as you can see the arrow pointing to it.  There is a button “Start Activity 2” on activity 1 screen, when this button is clicked then activity 1 will be suspended and it  state will be captured so that  a user can return back to it. Next activity 2  is started and and is pushed on to  the task backstack. There is a button on this activity as start activity 3. If user presses this button then activity 2 will be suspended and activity 3 will be pushed to the task backstack and similarly same happens when user presses the “Start Activity 4″button and activity 3 is suspended and activity 4 is pushed to the top of task backstack.

Now if user decides to go back to “Activity 3”  then activity 4 is killed and poped off the back stack and “activity 3” will be at the top of task backstack so android will resume this activity, restoring its state and will bring it back in the view on the device.

The Activity Life Cycle

Activities are created, suspended, resumed and destroyed as necessary when an application executes and some of these actions depend on the user action. some depend on android such as it can kill any activity that is suspended  due to low battery etc

The Activity Life Cycle States

Once an activity is created it can be in resumed / running state and in this state it is visible and user can interact with it.

Activity can be paused, it will be  partially visible (may be a popup comes before it) to user but cannot interact with it.

An activity can be stopped and when it is stopped it is no longer visible and android can terminate these activities know that it might needs to create these later when the user navigates  back to them.

Activities need to behave differently during their life cycle, for instance if activity is running an animation and a dialog box pops up in front of it and then activity with animation might needs to be paused and when user responds to the dialog and  then restart the animation, once that dialog activity is  finishes.

In order to support activities like this  android announces changes to activity by calling specific life cycle methods.

android activity lifecycle methods

These methods are

  • protected void onCreate(Bundle savedInstanceState)  – When activity is about to be created.
  • protected void onStart()  – When activity is about to be visible
  • protected void onResume()
  • protected void onPause()
  • protected void onRestart()
  • protected void onStop()
  • protected void onDestroy() – When activity is about to be  destroyed.

If we want to take some specific action when activity state is changed then we need to override the method in our activity.

Suppose we have an activity that starts and exits in a minute or so  the first its OnCreate method is called then onStart and then onResume method and activity is in running state.

Android Activity Life Cycle

Then android will call its onPause method and then onStop and then finally onDestroy method is called and at this point activity is completely dead.So in entire life cycle of this activity it starts from start of  onCreate and ends at  the end of onDestroy.

Now when this activity started it was not visible but at some point it became visible and after some time it became invisible as it was removed from the screen. When activity is about to become visible android calls onStart method or sometime onRestart method. and when activity is going to be invisible android calls the onStop method.

So we can think of  the visible life time of an activity as occurring between  the start of call to onStart() to the end of call to onStop.  And finally when activity is on screen a there is time when user can interact with the activity and there is time when he can’t. For example when a device goes to sleep, in that case user cannot interact with the activity any more even though  it is still the foreground activity.

So when activity is about to be ready for the user interaction android calls the onResume method. When activity about to be stop being able to user interact with activity then onPause method is called. So we can think of an activity ‘s visible & in foreground life time from start of call to onResume method and end of call to onPause method.

So that’s it for this topic in our next article we will look into more detail  of activity class methods.  So see you in next article in the series.

 

No Responses