Monday, March 2, 2015

Android Activity


What is an activity in android?
Every android application is compose of several android activity. Let me demonstrate how activity works.

This is the android home screen. This is the android menu screen. Let's select the Messaging Application. This is the Messaging Application's landing page or the Messaging Application's Main Activity. Activity consists of the applications UI (button, texview, etc). 
Let's select the New Message Button. The New Message Button opens another activity. Here we type our message and the recepient of the message. Let's call this Activity2. Let's press the BACK key. We are returned to the Main Activity. Let's press the HOME Key.
We are returned to the home screen. Select the RECENT key and verify that the application is still there. If you swipe it it will be removed. Let's Select the Messaging application again. The Messaging application will be displayed in our screen again.


The above example is a simple demonstration of what is an Android Activity and how it is being implemented in android. Now every activity, Main Activity and Activity 2 in our example, have a life cycle. They are created, started and destroyed. Here is the complete life cycle of an Android Activity from Android developers documentation.

Android Life Cycle of an Activity


1.Application is first launched

Main Activity's onCreate() ->Main Activity's onStart() ->Main Activity's onResume() are called.

2. Button is clicked opening Activity2.

Main Activity's onPause() -> Activity 2's onCreate() ->Activity 2's onStart() ->Activity 2's onResume() -> Main Activity's onStop() are called.

Note: At onStop() the system has the right to call onDestroy() if memory is needed elsewhere.

2. Button is clicked opening Activity2.

Main Activity's onPause() -> Activity 2's onCreate() ->Activity 2's onStart() ->Activity 2's onResume() -> Main Activity's onStop() are called.

Note: At onStop() the system has the right to call onDestroy() if memory is needed elsewhere.

Why is Activity 2's activities in between onPause() and onStop()?
According to android developers documentation when the activity is completely obscured by another activity onStop() is called. Therefore, for another activity to take place Activity 2's activities must start.

3. In Activity 2 a simple dialog box is opened.


Why nothing happened?
According to android developers documentation f an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations. What we implemented is a not an activity but a a dialog box.

4. In Activity2 Activity3 is opened implementing dialog theme.

Activity 2's onPause() -> Activity 3's onCreate() -> Activity 3's onStart() -> Activity 3's onResume()

5.Back button is pressed closing Activity 3.

Activity 3's onPause() -> Activity 2's onResume() -> Activity 3's onStop() -> Activity 3's onDestroy()

Why is onDestroy called?
According to android developers documentation if an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state. The system destroyed Activity 3 becuase probably it knows that when Activity3 is closed pressing the back button should reopen Main Activity.

6. Back button is pressed closing Activity 2.

Activity 2's onPause() -> Main Activity's onRestart() -> Main Activity's onStart() -> Main Activity's onResume() -> Activity 2's onStop() -> Activity 2's onDestroy()

7. Home button is pressed.

Main Activity's onPause() -> Main Activity's onStop()

Note: Pressing home button put the current activity at onStop so it can be resumed if reselected from running applications

8. The application is selected from running applications

Main Activity's onRestart() -> Main Activity's onStart() -> Main Activity's onResume()

9. Back is pressed returning to Android home screen.

Main Activity's onPause() -> Main Activity's onStop() -> Main Activity's onDestroy()