Saturday, 28 February 2015

Activity life cycle in android

 Overview

Android Activity is the basic building block of an Android App. It’s a window that contains the user interface of your application. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). But there can be activities without UI. In short, activity is a something that the user is able to see in an application and which is responsible for performing user interactions, operations etc.
Typically, one activity in an application is specified as the "main" activity, also called launcher activity, which is presented to the user when launching the application for the first time. Each activity in android has its own life throughout the application and so life-cycle methods. Below diagram illustrate about activity life-cycle in brief.

Acitivity Life Cycle Flow


OnCreate():-

Called when the activity is first created. This is where you should do all of your normal static set up — create views, bind data to lists, and so on. This method is passed a Bundle object containing the activity's previous state, if that state was captured (will see how to capture activity state lateron). Always followed by onStart().


onStart():-

Called just before the activity becomes visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden. We can also say the activity is partially visible to user in onStart().


onResume():-

Called just before the activity starts interacting with the user. At this point the activity is at the top of the activity stack, with user input going to it.
Always followed by onPause().


onPause():-

Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes to persistent data, stop animations, unregistering broadcast receivers and other things that may be consuming CPU or leads to memory leak, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns. 
Followed either by onResume() if the activity returns back to the front, or by onStop() if it becomes invisible to the user.


onStop():-

Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it. We can also say the activity is partially visible to user in onStop().
Followed either by onRestart() if the activity is coming back to interact with the user, or by onDestroy() if this activity is going away.


onRestart():-

Called after the activity has been stopped, just prior to it being started again. This method is always called after onStop() if and only if the app is not killed by system in onStop().
Always followed by onStart().


onDestroy():-

This is the method which will be called when your Activity is getting killed. This is the final call the Activity will receive in its Lifecycle.
When the user press back button on any Activity the foreground activity gets destroyed and control will return to the previous Activity.
But remember the fact, there is no guaranty that onDestroy will be called. Only when the system is low on resources or user press the back button or if you use finish() explicitly in your code, onDestroy gets called.

Let’s observe Activity life cycle methods under different circumstances.

When app launches

onCreate() --> onStart() -->  onResume()

When home button pressed

onPaused() --> onStop()

After pressed home button when again open app from recent task list or clicked on icon

onRestart() --> onStart() --> onResume()

When back button pressed and exit the app

onPaused() -- > onStop() --> onDestory()

When open app another app from notification bar or open settings

onPaused() --> onStop()

Back button pressed from another app or settings then used can see our app

onRestart() --> onStart() --> onResume()

When any dialog open on screen

onPause()

After dismiss the dialog or back button from dialog

onResume()

There are four states an activity can possibly exist:

Starting State
Running State
Paused State
Stopped state

Starting state involves:

Creating a new Linux process, allocating new memory for the new UI objects, and setting up the whole screen. So most of the work is involved here.

Running state involves:

It is the activity (state) that is currently on the screen. This state alone handles things such as typing on the screen, and touching & clicking buttons.

Paused state involves:

When an activity is not in the foreground and instead it is in the background, then the activity is said to be in paused state.

Stopped state involves:

A stopped activity can only be bought into foreground by restarting it and also it can be destroyed at any point in time.

The activity manager handles all these states in such a way that the user experience and performance is always at its best even in scenarios where the new activity is added to the existing activities


Now lets go practically into android life cycle.
I have written the two demo examples to understand the Android Life cycle in depth.
1) AndroidLifeCycleMine:- 






























                                        You can find the source code here ActivityLifeCycleProjectMine





2) AndroidLifeCycle demo in multiple situation:- 
This example iulstrate in brief about Activity states when another Activity comes in picture and when dialog appears on the Activity.
 

 

You can find the source code here ActivityLifeStates in different situations.

Now look towards Activity in a professional way. Because the professional minds are always key to "Invention". Below are some points about this topic which you may come across during an interview.

 What will be the Activity state when you press the menu option.

 Ans:- Paused state - and life cycle method onPause().

What if you call finish() method in various Activity callback methods.

 Ans:-  If finish() is called in oncreate() then directly onDestroy() will be called.

If  finish() is called in onStart() then onResume() will not be called and Obviously if onResumed is not called then onPause() will not be called it means after onStart() directly() onStope() and onDestroy() will called().

If finish() is called in onResume() then all life cycle methods followed by onResume() will be called.

If finish() is called in onPause() then all life cycle methods followed by onPause() will be called means onStope() and onDestroy().

If finish() is called in onStope() then all life cycle followed by onStope() will be called.

The onCreate() and onDestroy() methods are called only once throughout the activity lifecycle.

If you have any point about Activity which you feel the professional minds should know then please comment it.


No comments:

Post a Comment