Activity Lifecycle

Activity Stack

            For each running application, the android runtime system maintains an activity stack. When an application is launched, the first of the application’s activities to be started is placed onto the stack. When a second activity is started, it is placed on the top of the stack and the previous activity is pushed down. The activity at the top of the stack is referred to as the active (or running) activity. When the active activity exits, it is popped off the stack by the runtime and the activity located immediately beneath it in the stack becomes the current active activity.

Activity Lifecycle

            As a user interact with app, the android system will call various lifecycle callback methods on activity. As a user navigates through, out of, and back to your app, the activity instances in your app transition through different states in their lifecycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.


The Activity class provides a core set of six callbacks: onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). The system invokes each of these callbacks as an activity enters a new state.

onCreate()This callback gets fired when the system first creates the activity. In the onCreate() method, perform basic application startup logic that should happen only once for the entire life of the activity. Activity does not reside in the created state. After the onCreate() method finishes execution, the activity enters the started state, and the system calls the onStart() and onResume() methods in quick succession.

onStart() - The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive. Once this callback finishes, the activity enters the Resumed state, and the system invokes the onResume() method.

onResume() - This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app. When an interruptive event occurs, the activity enters the Paused state, and the system invokes the onPause() callback.

onPause() -  The system calls this method as the first indication that the user is leaving your activity. There are several reasons why an activity may enter this state. Such an event might be, for instance, receiving a phone call, the user’s navigating to another activity, or the device screen’s turning off. Completion of the onPause() method does not mean that the activity leaves the Paused state. Rather, the activity remains in this state until either the activity resumes or becomes completely invisible to the user. If the activity resumes, the system once again invokes the onResume() callback. If the activity returns from the paused state to the resumed state, the system keeps the Activity instance resident in memory, recalling that instance when it the system invokes onResume(). If the activity becomes completely invisible, the system calls onStop(). 

onStop() - When your activity is no longer visible to the user, it has entered the stopped state, and the system invokes the onStop() callback. This may occur, for example, when a newly launched activity covers the entire screen. The system may also call onStop() when the activity has finished running, and is about to be terminated. In the onStop() method, the app should release almost all resources that aren't needed while the user is not using it.

onDestroy() - Called before the activity is destroyed. This is the final call that the activity receives. The system either invokes this callback because the activity is finishing due to explicitly calling finish(), or because the system is temporarily destroying the process containing the activity to save space.

Comments

Popular posts from this blog

Android Process States

Android Overview

Android App Overview