Home >Common Problem >activity life cycle methods
Recently I reviewed the life cycle of Activity, read relevant books and official documents, and gained a lot. My previous understanding has been greatly improved, and I would like to share it with you here.
Activity is equivalent to a servlet. Our Activity is in this container. All processes such as creating instances, initializing, and destroying instances are called by the container. This is The so-called "Don't call me, I'll call you." mechanism. (Recommended learning: Java Video Tutorial)
Let’s take a look at this classic life cycle flow chart:
onCreate():
This method will be automatically run when the activity is created. This method does some initialization actions, such as creating views, setting data to the list, etc. This method provides a Bundle type variable, which contains the previous status information of the activity, provided that this information has been saved before. After this method is executed, the onStart() method is executed; if the finish() method is added to the onCreate method, onCreate will run the onDestroy method
onRestart():
When the activity wakes up from the onStop state, the onRestart method will be used. This method takes precedence over onStart that is run again. OnStart is run after onRestart is completed. If you add a finish() statement to the onRestart() method, onStart and subsequent state methods will continue to run until onDestroy finishes running
onStart():
When the activity is onStart will be called when the user is visible. When the activity is displayed in the foreground, onResume will be run. When the activity is hidden (stopped) before it is displayed in the foreground, onStop() will be run. For example, the finish() method is used in the onStart method. If so, onStop->onDestroy will be run directly after onStart().
onResume():
onResume is called when the activity begins to interact with the user, and the activity is at the top of the activity stack for user operations. After certain operations, what is executed after the method is executed is onPause()
onPause():
When an activity runs to the onResume method, whether the activity is to be destroyed This method will be called whether you want to pause or stop. This method may be followed by onResume or onStop. If a new activity-B is opened in this activity-A that does not completely cover this activity-A, then activity-A will be in the onPause state. When activity-B exits , activity-A will directly run onResume (provided that the onPause method has been executed, otherwise it will wait for the onPause method to be executed before running the onResume method, so it is not recommended to perform CPU-intensive operations in this method). If activity-A needs to be exited, onStop will be executed next. onPause() is used to submit unsaved persistent data that has changed, and to stop animations and other events that consume more CPU (such as broadcast receivers, sensors (such as GPS), or resources that consume power). This is for the purpose of updating OK, run the new activity
onStop():
When this activity is completely invisible, the onStop method will be called, because another activity will call onResume and overwrite this activity . The following three situations will cause this activity to call the onStop() method. The first is that a new activity is executed, the second is that an existing activity is switched to the front, and the third is that the activity is destroyed. If the activity is recalled by the user, the onRestart method will be called; if the activity is to be destroyed, the onDestroy method
onDestroy():
will be called before the activity is destroyed. Method, for example, if the following happens: the activity calls the finish() method to end the activity, or the system temporarily destroys the activity to save space, these two situations can be judged by the isFinishing() method
More For Java-related technical articles, please visit the Java Development Tutorial column to learn!
The above is the detailed content of activity life cycle methods. For more information, please follow other related articles on the PHP Chinese website!