Introduction and basics of android activity

Android Activity and Activity Lifecycle is one of the most basic concept while developing application on Android platform.

What is Android Activity?
  • An activity is a single, focused thing that the user can do.
  • An activity represents a single screen.
  • 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).
  • Activities can also be used in other ways: as floating windows ( via a theme with windowIsFloating set) or embedded inside of another activity ( using AcitivityGroup). 
  • There are two methods almost all subclass of Activity will implement: onCreate(Bundle) and onPause().

Activity Lifecycle
  • Activities in the system are managed as an activity stack
  • When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

An activity has essentially four states:
  1. Active or Running
  2. Paused
  3. Stopped
  4. Shutdown

The following diagram shows the important state paths of an Activity.
  • The square rectangles represent callback methods you can implement to perform operations when the Activity moves between states. 
  • The colored ovals are major states the Activity can be in.




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, etc. 
  • This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. 
  • Always followed by onStart().

onRestart() :

  • Called after your activity has been stopped, prior to it being started again. 
  • Always followed by onStart()

onStart() :

  • Called when the activity is becoming visible to the user. 
  • Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

onResume() :

  • Called when the activity will start interacting with the user. 
  • At this point your activity is at the top of the activity stack, with user input going to it. 
  • Always followed by onPause().

onPause ():

  • Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. 
  • The counterpart to onResume(). When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

onStop():

  • Called when you are no longer visible to the user. 
  • You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.
  • Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

onDestroy() :

  • The final call you receive before your activity is destroyed. 
  • This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. 
  • You can distinguish between these two scenarios with the isFinishing() method.

When the Activity first time loads the events are called as below:
  • onCreate()
  • onStart()
  • onResume()

When you click on Phone button the Activity goes to the background & below events are called:
  • onPause()
  • onStop()

Exit the phone dialer & below events will be called:
  • onRestart()
  • onStart()
  • onResume()

When you click the back button or try to finish() the activity the events are called as below:
  • onPause()
  • onStop()
  • onDestroy()

No comments:

Post a Comment