A Tutorial On The Insight of Activities In Android

imgJuly 4, 2017
 

An activity is single focused action which the user can take. Like for example, an activity may present a list of menu items which the user can choose from or cam display photographs with the captions. An application may consist of only one activity or several. Activities may work together for appearing in one cohesive application or can work independently from each other. Any activity in Android is important part of any application’s overall life cycle and the way various activities are launched and put together is surely a fundamental aspect of the Android application model. Every activity is implemented right as an implementation of the Activity base class. All activities usually interact with the user, so the Activity class will create the window in which one can place the user interface (UI). Activities are presented in full-screen mode in many instances one can find activity floating in window. Activities are presented in full-screen mode with instances one can find activity floating in window or embedded inside another activity known as activity group.

Working with methods, stacks, and states

Two important methods where most activities are implemented are

  • onCreate: This is where activity is initialized. Most importantly, it is where one can tell the activity in which layout to use with using layout resource identifier which is considered the entry point of the activity.
  • onPause: This is the case where one deal with the user leaving the activity. Any changes made by the user certainly should be committed right at this point.

Activities in the system are usually managed right as an activity stack. When a new activity is created it is placed right on the top of the stack and it becomes the running activity. There are previous running activity which remains below the stack and returns right to the foreground with the new activity exits.

An activity has four states- Essential States of an Activity

Active/running

The activity is in the foreground of the screen right at the top of the stack.

Paused

In this case, the activity has lost all focus but it is still visible. Because a paused activity is alive, it can maintain state as well as member information and remain attached right to the window manager which is present in Android. With Gingerbread (3.0), the activity can surely be killed by Android system in low memory conditions.

Stopped

If an activity becomes obscured by another activity, then it is stopped. It will retains all the state and member information, but it is not visible right to the user. Therefore, the window is hidden and will kill by the Android system when the memory is need somewhere else.

Created and resumed

The system has either stopped or paused the activity. The system can certainly reclaim the memory by asking it to finish or can kill the process. When it displays the activity right to the user, it must start by restoring and restarting right to the previous state.

Tracking an activity’s life cycle

The diagram below represents the important paths of the activity- the activity life cycle. The rectangles will represent callback methods which one can implement for responding the events in the activity. The shaded ovals will represent the major states of the activity. The activity life cycle is large and complex topic with the sections covering the basics only.

Monitoring key loops

One may be interested in monitoring these three loops right in the activity:

  • The entire lifetime will take place right between the first call to onCreate() and the final call to onDestroy(). The activity will perform all the global setup in onCreate() and will release the remaining resources in onDestroy(). For example, if one create a thread to download file from internet in the background, it may be started in the onCreate() method. That thread can be stopped in the onDestroy() method.
  • The visible lifetime of the activity will take place between the onStart() and onStop() methods. Right during this time, the user can see the activity onscreen. Between these two methods, one can maintain the resources which needed to show and run activity. For example, one can create event handler for monitoring the state of the phone. The phone state can change and the event handler can inform the activity of the phone from entering the Airplane mode and then react accordingly. The event handler in onStart() and tear down any resources one is accessing in onStop(). The onStart() and onStop() methods can be called multiple times as the activity will become visible or hidden in the user.
  • The foreground lifetime of the activity will begin at the call to onResume() and ends at the call to onPause(). Right during this time, the activity is in front of all other activities and is interacting with the user. An activity will normally toggles between onResume() and onPause() multiple times, for example when the device goes to sleep or when a new activity handles a particular event- the code in these methods must be lightweight.

Viewing activity methods

The entire activity life cycle boils down to these methods:

public class Activity extends ApplicationContext {
protected void onCreate(Bundle savedInstanceState);
protected void onStart();
protected void onRestart();
protected void onResume();
protected void onPause();
protected void onStop();
protected void onDestroy();
}

The methods can be overridden and custom code can be placed in all of them. The activities will implement onCreate() for initialization and may also implement onPause() for clean-up. One should call the superclass( base class) when implementing these methods.

Following an activity’s path

The movement of the activity throughout its life cycle will look like:

  • onCreate(): Called when the activity is first created. One initializes most of the activity’s class-wide variables here. onStart() is always called next. Killable: No.Next: onStart().
  • onRestart(): Called after the activity has been stopped right before it started again. onStart() is always called next. Killable: No. Next: onStart
  • onStart(): Called when the activity is visible to the user. This one is followed by onResume() if the activity is brought to the foreground or onStop() if it becomes hidden from the user. Killable: No. Next: onResume() or onStop().
  • onResume(): Called when the activity is available for interacting with the user. The activity is at the top of the activity stack at the point. Killable: No. Next: onPause().
  • onPause(): Called when the system is about to resume right from a previous activity or if the user has navigated away to another portion of the system such as by pressing the Home key. This stage is typically used for committing unsaved changes to data which needs to be persisted. If the activity is brought back to the foreground, onResume() is called; if the activity is invisible to the user, onStop() is called. Killable: Yes, but only on Gingerbread (2.3) or earlier. Next: onResume() or onStop().
  • onStop(): Called when the activity is no longer visible to the user because another activity has resumed and is covering this one. This may happen because another activity has started or previous activity has resumed and is now in the foreground of the activity stack. It is followed by onRestart() if this activity is returning to interact with the user or by onDestroy() if this activity is going away. Killable: Yes. Next: onRestart() or onDestroy().
  • onDestroy(): The final call you receive right before the activity is destroyed. This method gets called either because the activity is finishing or because the system is temporarily destroying the activity to reclaim space. One can distinguish between these two with the isFinishing() method, which helps identify whether the method is finishing or the system is killing it. The isFinishing() method is often used inside onPause() to determine whether the activity is pausing or getting destroyed. Killable: Yes. Next: Nothing.

Leave a Reply

Your email address will not be published. Required fields are marked *