Every Android developer should know that the Android system has four important basic components: activities, Services, Broadcast Receive, and Content Providers. Activity is the most important component. When you open an APP on your phone, all you see is Activity. Here are some questions about the Activity life cycle.

 

1 Eight callback functions for the Activity lifecycle

The diagram below is the life cycle diagram of an Activity. I believe many people have seen it more than once, but some people understand it when they see it. After a period of time, they will not remember or forget it.



It’s as simple as remembering these two things:

(1) The Activity’s six callbacks onCreate() — onStart() — onResume() — onPause() — onStop() — onDestroy() can be divided into three groups. OnResume () and onPause() indicate whether the Activity is in focus in the foreground, onStart() and onStop() indicate whether the Activity is visible, and onCreate() and onDestroy() indicate whether the Activity exists. The OnRestart() function is not called when the Activity is started for the first time. OnRestart() is called first and then onStart() when the Activity returns from the invisible onStop() state. In addition to these seven callbacks, there is another important function: OnSaveInstanceState (), which is often used to hold status variables of Actitity.

(2) When an Activity starts, it stops after the onResume() function. Imagine if you open an Android APP and you’re looking at the content displayed on an Activity. If the Activity prints debug information and you can see it, The approximate information would look something like this



When you look at the content of the Activity, the Activity stops after the onResume function.

 

2 Activity several important callback function description

Which of the Activity’s eight callbacks are the most important?

OnCreate () is by far the most important. Ides generate onCreate() by default, which is called when an Activity is first created. OnCreate () loads the layout, initializes the view, binds events, and does a variety of other initialization operations. An Activity’s other callbacks may not be called, but onCreate() will be called. Since onCreate() contains a lot of stuff, it is not maintainable to write all the code at once. Instead, it should be divided into subfunctions like the following:

[java] 
view plain
 copy

  1. protected  voidonCreate(Bundle savedInstanceState) {  
  2.        super .onCreate(savedInstanceState);  
  3.        setContentView(R.layout.activity);  
  4.                   f1();  
  5.                   f2();  
  6.                   f3();  
  7. .
  8. }  





OnPause () is the first method to be called when you leave an Activity. You should save some important data in onPause() to free up system resources, because onPause() cannot perform time-consuming operations until the next Activity is finished.

OnSaveInstanceState () is often used to hold temporary state information about an Activity, and is useful when a change in system configuration or low memory causes the Activity to be rebuilt. For example, if the information we enter in EditText is lost when the screen rotates, we can save the user’s input in onSaveInstanceState() and restore it in onCreate() or onRestoreInstanceState(). The latter is more convenient because Null judgment is not required.

3 Other small questions

(1) About super.onCreate(savedInstanceState)

You’ll notice that each callback first calls the super.onx () callback of the parent class. This is a must. If the parent callback is not called, the runtime application will crash.

(2) Why don’t you see the Activity constructor

In fact, you can write an Activity constructor. The no-argument constructor does execute before onCreate(), but the constructor doesn’t really do much. The system creates the context after the Activity constructor executes, and then calls onCreate(). All initialization operations should be placed in onCreate().

(3) When onPause() is called but not onStop()

OnPause () indicates that the Activity loses focus, onStop() indicates that it is not visible, OnPause () can only be called without onStop() if the Activity is partially visible but has lost focus. However, onPause() is not always called when the Activity is partially visible and has lost focus, such as the current dialog generated by the Activity, the system shutdown dialog, or the volume dialog. Both take the current Activity out of focus, but neither calls onPause().

How do I call onPause() without calling onStop? You can create two new activities, start Activtiy2 with Activity1, and set the theme of Activity2 to

android:theme=”@style/Theme.AppCompat.Dialog”

You’ll notice that Activity1 will only call onPause() and not onStop.

When you open a url in your Activity, the system will pop up a dialog box with several applications to choose from, such as UC browser or QQ browser, and only onPause() will be called, but onStop() will not be called. Specific methods we can test their own experiments.

These are a few questions about the Activity life cycle, and we’ll discuss more about activities later.

Reprint please indicate the source http://blog.csdn.net/thewalker3000/article/details/61197825

Welcome to leave your comments.