Chúng tôi biết làm thế nào để một ứng dụng hoạt động trong nền tảng desktop(desktop platform) như
Windows, Linux hoặc Mac. Sau khi bắt đầu một chương trình, chúng ta có thể giảm thiểu hoặc khôi phục nó bất cứ khi nào chúng ta muốn và the software will preserve its state (trừ khi hệ thống treo - unless the system crashes). Tình hình là hoàn toàn khác trong Android và chúng ta có quyền kiểm soát it hơn trong Android. Do đó như một nhà phát triển, chúng ta cần một kiến thức toàn diện về vòng đời "Android Activity" trước khi bắt đầu phát triển.
Một Activity trong Android có thể tồn tại trong 4 trạng thái được mô tả như sau :
1. Active/Running state
Đây là trạng thái khi mà một activity đang xuất hiện trên màn hình và người dùng có thể nhìn thấy, tương tác với nó.
2. Paused state
Trong trạng thái dừng lại, activity hiển thị một phần với người dùng nhưng không hoạt động và mất tập trung vào nó. This occurs when some another Activity is on top of this one which doesnot cover the entire screen or having some transparancy so that the underlying Activity is partially visible. A paused activity is completely alive and maintains its state but it can be killed by system under low memory when memory can be freed by no other ways.
3. Stopped state
This is when the Activity is no longer visible in the screen. Another activity is on top of it and completely obscures its view. In this state also the activity is alive and preserves its state, but more likely to be killed by the system to free resources whenever necessary
4. Destroyed/Dead state
An Activity is said to be dead or destroyed when it no longer exists in the memory. Either the Activity hasn’t been started yet or once it was started and killed by the system in Paused or Stopped state to free resources.
The Android Activity Lifecycle Diagram
The figure below shows the Android Activity Lifecycle flow chart. Its very important for a developer, have a close study.The gray rectangular boxes shows the callback methods that are called prior to the state changes.
The above Android Activity Lifecycle flow diagram can be explained as follows:
- When we launch an Activity in Android, it first calls the onCreate() method. This is where we do User Interface creation and initialization of data elements. This method is provided with a Bundle object as parameter to restore the UI state.
- onStart() method is called before the Activity is being visible to the User. Remember that Activity is still not Active.
- With the onResume() method, the Activity become visible and Active for the user to interact with. The Activity will be at the top of the Activity stack at this point. Now the Activity is in running /active state and is able to receive user inputs.
- In the Active state, onPause() method will be called when the system is about to resume another Activity on top of this one or when the user is about to navigate to some other other parts of the system. It is the last guaranteed call to a method before the Activity can get killed by the system. That is, there’s a possibility that your activity may be killed by the system at the paused state without executing any further method calls. Therefore it is important to save the user interface configuration and critical data at this method.
- By default, an Activity can remain in the paused state if:
- The user has pressed the home button
- Another activity or notification which is on top of it doesn’t completely obscures the visibility of underlying Activity.
- The device goes to sleep.
- There are three possibilities for an Activity under paused state:
In all other cases it goes to stopped state by executing onStop() method. This is the default action when the user has pressed the back button, or a new activity which completely covers it resumes on top.
- The user resumes the Activity by closing the new Activity or notification and the paused Activity gets Active/Running by calling onResume() method.
- It gets killed by the system under extremely low memory conditions. In this case there will be no further method calls before the destruction of the Activity and it needs to be re-run from the beginning by calling onCreate() and restoring the previous configuration from bundle object.
- An Activity under stopped state also has three different scenarios to happen:
- System kills it to free resources. An activity under stopped sate is more likely to be killed by the system than one in the paused state. It needs to start the cycle again with onCreate().
- It get restarted by calling onRestart() , onStart() and onResume() methods in the order if the user navigates back to the Activity again. In this case, the User Interface is intact and no need to be restored.
- onDestroy() method is called and the Activity is destroyed. This is the final method we can call before the Activity is destroyed. This occurs either because the Activity is finishing the operation or the system is temporarily destroying it to save space..
Android Activity Lifecycle Loops
By analyzing the Android Activity lifecycle diagram we can see there are three lifecycle loops exist for every Activity and are defined by those callback methods.They are:
Entire Lifetime: This is the lifetime between the first call to the onCreate() and the final call to onDestroy() method. We create all global resources such as screen layout, global variables etc in onCreate() and release all resources with onDestroy() call.
Visible Lifetime: It is the lifetime of an Activity between onStart() and onStop()
method calls. In this the Activity is visible to the user and he may or
may not be able to interact with it. During the visible lifetime, an
Activity maintains its state intact.
Foreground Lifetime:
Foreground lifetime starts with onResume() and ends with onPause()
method calls. During this, the Activity is completely visible to the
user and is on top of all other Activities so that user can interact
with it.
Add your comments and feedback below: