In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the life cycle of Activity components in Android". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the life cycle of components Activity in Android".
Catalogue
Return to the stack
Activity statu
1. Running state
two。 Paused state
3. Stop state
4. Destroy statu
The lifetime of Activity
OnCreate ()
OnStart ()
OnResume ()
OnPause ()
OnStop ()
OnDestroy ()
OnRestart ()
Complete survival time
Visible survival time
Foreground lifetime
Activity recovery treatment
Return to the stack
The Activity in Android can be cascaded. Every time we start a new Activity, it will overwrite the original Activity. Then clicking the Back key will destroy the top Activity, and the following Activity will be displayed again.
In fact, Android uses task to manage Activity. A task is a collection of Activity stored in the stack, also known as the return stack.
By default, start a new Activity, and it will be stacked on the return stack and at the top of the stack. Every time we press the Back key or call the finish () method to destroy an Activity, the Activity at the top of the stack will be off the stack, and the previous Activity will be back at the top of the stack. The system always displays the Activity at the top of the stack to the user.
Activity statu
Each Activity may have up to four states in its lifecycle:
1. Running state
Activity is running when an Activity is at the top of the return stack
two。 Paused state
When an Activity is no longer at the top of the stack but is still visible, the Activity enters a paused state
3. Stop state
When an Activity is no longer at the top of the stack and is completely invisible, it enters a stop state
4. Destroy statu
An Activity becomes destroyed after it is removed from the return stack
The lifetime of Activity
Seven callback methods are defined in the Activity class, covering every step of the Activity lifecycle:
OnCreate ()
It is called when the Activity is first created, and you can complete the initialization of the Activity in this method, such as loading layout, binding events, etc.
OnStart ()
This method is called when the Activity changes from invisible to visible
OnResume ()
This method is called when the Activity is ready to interact with the user, and the Activity must be at the top of the return stack and running
OnPause ()
This method is called when the system is ready to start or restore another Activity. We usually release some resources that consume CPU and save some critical data in this method, but the execution speed of this method must be fast, otherwise it will affect the use of the new top stack Activity.
OnStop ()
The main difference between this method and the onPause () method, which is called when the Activity is completely invisible, is that if the new Activity launched is a dialog Activity, the onPause () method is executed, while the onStop () method is not executed
OnDestroy ()
This method is called before the Activity is destroyed, after which the state of the Activity becomes destroyed.
OnRestart ()
This method is called before Activity changes from stopped to running, that is, Activity is restarted
Except for the onRestart () method, all of the above seven methods are opposite to each other, so the Activity can be divided into the following three lifetimes:
Complete survival time
What Activity goes through between the onCreate () method and the onDestroy () method is the full lifetime. In general, an Activity completes various initialization operations in the onCreate () method and frees memory in the onDestroy () method.
Visible survival time
What Activity goes through between the onStart () method and the onStop () method is the visible lifetime. During the visible lifetime, Activity is always visible to the user, even though it may not be possible to interact with the user. Through these two methods, we can reasonably manage the resources that are visible to the user. For example, the resource is loaded in the onStart () method and freed in the onStop () method to ensure that the Activity in the stopped state does not take up too much memory
Foreground lifetime
What Activity goes through between the onResume () method and the onPause () method is the foreground lifetime. During the lifetime of the foreground, the Activity is always running, and the Activity can interact with the user. What we usually see and contact most is the Activity in this state.
Activity recovery treatment
When an Activity enters a stopped state, it is possible to be recycled by the system. If an Activity is reclaimed due to insufficient system memory, the Activity will display normally when the user returns it, but the onRestart () method will not be executed, but the onCreate () method of Activity will be executed, because the Activity will be recreated in this case
If there is temporary data and status in Activity, for example, the text input box has already entered a paragraph of text, and the text is gone when you come back, it will affect the user experience. Activity provides an onSaveInstanceState () callback method, which ensures that the Activity will be called before it is reclaimed. This method can be used to solve this problem.
The onSaveInstanceState () method takes a parameter of type Bundle, and Bundle provides a series of methods for saving data.
Override fun onSaveInstanceState (outState: Bundle) {super.onSaveInstanceState (outState) var tempData = "Something you just typed" outState.putString ("data_key", tempData)}
After the data is saved, there is a Bundle type parameter in the onCreate () method, which has all the previously saved data. You only need to take out the data through the corresponding value method.
Override fun onCreate (savedInstanceState: Bundle?) {super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) if (savedInstanceState! = null) {val tempData = savedInstanceState.getString ("data_key")}} Thank you for reading. This is the content of "how to understand the Lifecycle of Activity components in Android". After the study of this article I believe you have a deeper understanding of how to understand the life cycle of Activity components in Android, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.