Pages

Friday, August 24, 2012

Activity. What is that???

Android application can constructed for many purpose. It can be an activity, a service, a broadcast receiver or can be content providers. So lets get some focus on Activity.

Activity: An activity is a single, focused thing that user can do. User interacts with all activities in an android phone, so the Activity class takes care of creating a window in which user can place UI with setContentView(view). Activities are presented to user in many ways like, Full screen windows, Floating windows, or embedded inside another activities.

The Activity class is an important part of an application's overall lifecycle, and the way activities are launched and put together is a fundamental part of the platform's application model.

Now, lets understand Application fundamentals.

1. Each screen in an Android application is an activity, and user can perform actions by interacting with visual components on the activity. It is like a web site where you can perform some actions on one page but you have to move to another page to access components of that page.

2. Each activity in an Android application is a direct subclass of Activity base class or a subclass of an Activity subclass that provides specialized functionality, and is represented in the application package as a single Java class.

3. All activities in application must be defined in AndroidManifest.xml file (We'll learn it in detail), an XML file that keeps all the information of your Android application like, components, resources, and permissions that make up the application.

4. All Android application components, including activities, have lifecycles. The current lifecycle state of a component provides part of the information the Android OS uses when it determines what components can be safely removed from memory in order to free up memory space. All activities inherit a set of lifecycle methods from the Activity base class that are executed when the corresponding lifecycle state of the activity is reached.

 Take a look at Activity life cycle.
http://developer.android.com/reference/android/app/Activity.html 


Above digram directly linked from official website of Android.

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:
          1. onCreate()
          2. onStart()
          3. onResume()

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

Exit the phone dialer & below events will be called:
          1. onRestart()
          2. onStart()
          3. onResume

When you click the back button OR try to finish() the activity the events are called as below:
          1. onPause()
          2. onStop()
          3. onDestroy

Activity states
The Android OS uses a priority queue to assist in managing activities running on the device. Based on the state a particular Android activity is in, it will be assigned a certain priority within the OS. This priority system helps Android identify activities that are no longer in use, allowing the OS to reclaim memory and resources. The following diagram illustrates the states an activity can go through, during its lifetime:

These states can be broken into 3 main groups as follows:

Active or Running - Activities are considered active or running if they are in the foreground, also known as the top of the activity stack. This is considered the highest priority activity in the Android Activity stack, and as such will only be killed by the OS in extreme situations, such as if the activity tries to use more memory than is available on the device as this could cause the UI to become unresponsive.

Paused - When the device goes to sleep, or an activity is still visible but partially hidden by a new, non-full-sized or transparent activity, the activity is considered paused. Paused activities are still alive, that is, they maintain all state and member information, and remain attached to the window manager. This is considered to be the second highest priority activity in the Android Activity stack and, as such, will only be killed by the OS if killing this activity will satisfy the resource requirements needed to keep the Active/Running Activity stable and responsive.

Stopped - Activities that are completely obscured by another activity are considered stopped or in the background. Stopped activities still try to retain their state and member information for as long as possible, but stopped activities are considered to be the lowest priority of the three states and, as such, the OS will kill activities in this state first to satisfy the resource requirements of higher priority activities.

No comments:

Post a Comment