Getting Started with Android (iii) Activity-life cycle and startup mode
Source: Internet
Author: User
<span id="Label3"></p><p><p>Original Link: http://www.orlion.ga/432/</p></p><p><p>I. Life cycle of activities</p></p><p><p>1. Return stack</p></p><p><p>The activities in Android can overlap, each start a new activity, we will overwrite the original activity, and then click the Back button will destroy the top activity, the following activity will be displayed again, Android is using tasks to manage the activity, A task is a set of activities placed on the stack, which is also a return stack. By default we start a new activity and it is placed in the stack at the top of the stack in the return stack. When we press the back button or call finish</p></p><p><p>() method destroys an activity, the activity at the top of the stack will be out of the stack, and then the previous stack activity will be back on top of the STACK. The system always shows the activity at the top of the stack to the user</p></p><p><p>2. Activity Status</p></p><p><p>Each activity may have a maximum of four states in its life cycle</p></p><p><p>(1). Operational Status</p></p><p><p>When an activity is at the top of the stack, it is in the running State.</p></p><p><p>(2). Pause Status</p></p><p><p>Paused when an activity is no longer in the top position of the stack, but is still visible. You may think that since the activity is not at the top of the stack, how can it be visible? This is because not every activity will fill the entire screen, such as dialog-box activities that occupy only a portion of the middle of the Screen. The suspended activity is still completely alive and the system is reluctant to recycle it.</p></p><p><p>(3). Stop State</p></p><p><p>When an activity is no longer in the top position of the stack and is completely invisible, it enters the stop State. The system will still save the appropriate state and member variables for this activity, but this is not completely reliable, and when memory is needed elsewhere, the activity in the stopped state may be reclaimed by the System.</p></p><p><p>(4). Destroy State</p></p><p><p>When an activity is removed from the return stack, it becomes a destroyed State. The system will most likely recycle activities in this state, thus ensuring that the phone has sufficient memory.</p></p><p><p></p></p><p><p>3. The lifetime of the activity</p></p><p><p>The activity class defines seven callback methods that cover every aspect of the active life cycle</p></p><p><p>(1) onCreate ()</p></p><p><p>This method is called when the activity is first created, and should be done in this method, such as loading layouts, binding events, and so On.</p></p><p><p>(2) OnStart ()</p></p><p><p>This method is called when the activity becomes visible from invisible</p></p><p><p>(3) Onresume ()</p></p><p><p>This method is called when the activity is ready and the user Interacts. At this point the activity must be at the top of the stack at the back of the stack, in active State.</p></p><p><p>(4) OnPause ()</p></p><p><p>This method is called when the system is ready to start or reply to another Activity. We usually release some cpu-consuming resources in this method and save some key data, but this method must be executed quickly, otherwise it will affect the use of the new stack top.</p></p><p><p>(5) OnStop ()</p></p><p><p>This method is called when the activity is completely invisible. The main difference between it and the OnPause method is that if the new activity started is a dialog-box activity, then the OnPause () method will be executed, and OnStop () will not execute</p></p><p><p>(6) OnDestroy ()</p></p><p><p>This method is called before the activity is destroyed, and then the state of the activity becomes the destruction state</p></p><p><p>(7) Onrestart ()</p></p><p><p>This method is called before the activity is changed from the stop state to the running state, i.e. the activity is Restarted. Seven of the above methods are handled</p></p><p><p></p></p><p><p>The Onrestart () is 22 relative, so the activity can be divided into three lifetimes</p></p><p><p>(1). Full Lifetime</p></p><p><p>The activity between the OnCreate () method and the Ondestory () method is the complete survival period.</p></p><p><p>(2). Visible Lifetime</p></p><p><p>The activity that is experienced between the OnStart () method and the OnStop () method is the visible Lifetime. During the visible lifetime, the activity is always visible to the user, and the distortion may not interact with the User. Through these two methods, we can reasonably manage the resources that are visible to the User. For example, loading a resource in the OnStart () method and freeing the resource in the OnStop () method to ensure that the activity from the stopped state does not consume too much memory</p></p><p><p>(3). foreground lifetime</p></p><p><p>Activity between the Onresume () method and the OnPause () method, is the foreground lifetime, in the foreground lifetime, the activity is always in the running state, at this time the activity is able to interact with the User.</p></p><p><p>The entire life cycle</p></p><p><p></p></p><p><p></p></p><p><p>4, The activity is recycled how to do</p></p><p><p>When an activity enters the stop state, it is possible to be reclaimed by the system, if there is an activity a in the application, the user starts activity B on the basis of activity a, the activity a goes to the stop state, this time because the system memory is not enough, the activity A is recycled and then the user presses the return key to return to activity A. In fact, it will normally show activity a, but this is not going to execute the Onrestart () method, but it will execute the OnCreate () method of a, because activity a will be recreated in this case.</p></p><p><p>But! There may be temporary data and status in activity a, such as a text input box in mainactivity, now enter a text, and then start normalactivity, mainactivity because the system memory is not enough to be recycled, After a while and then click the Back button back to Mainactivity will find the text just entered is gone, because Mainactivity was Re-created. This is very detrimental to the user experience. The activity provides a onsaveinstancestate () callback method, which guarantees that it must be called before the activity is recycled, so we can use this method to solve the problem of not saving the temporary data for the active Collection.</p></p><p><p>The Onsaveinstancestate () method carries a bundle type parameter, and the bundle provides a series of methods for saving the data, such as using the Putstring () method to save the string, and using the Putint () method to save the integer number ... Each save method needs to pass in two parameters, the first parameter is the key, which is used to take the value from the bundle behind, and the second parameter is the data that is actually being saved.</p></p><p><p>You can save temporary data by adding the following code to the Mainactivity:</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">@Overrideprotected void onsaveinstancestate (Bundle Outstate) {super.onsaveinstancestate (outstate); String TempData = "this is temp data"; outstate.putstring ("tempdata", tempdata);}</pre></pre><p><p>The data has been saved, How should we recover it? The OnCreate () method we have been using has a parameter of type Bundle. This parameter is null in general, but when the activity is saved by the Onsaveinstancestate () method before the event is reclaimed by the system, This parameter will have all the data that was previously saved, and we just need to use the appropriate method to remove it. To modify the OnCreate method for Mainactivity:</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">If (savedinstancestate! = Null) {String tempdata = savedinstancestate.getstring ("tempdata"); LOG.D ("tag", tempdata);}</pre></pre><p><p>Remove the value and then perform the related recovery Operation.</p></p><p><p></p></p><p><p></p></p><p><p>second, the activation mode of the activity</p></p><p><p>There are four types of start-up modes, namely Standard,singletop,singletask,singleinstance. You can select the startup mode in Androidmanifest.xml by assigning the Android:launchmode property to the <activity> Tag.</p></p><p><p>1. Standard</p></p><p><p>The default startup mode, Android is to use the return stack to manage the activity, in standard mode, whenever a new activity is started, it will be in the back of the stack, and at the top of the stack, for the use of standard mode of activity, The system does not care if the activity already exists in the return stack, and a new instance of the activity is created each time it is Started.</p></p><p><p>2, Singletop</p></p><p><p>When the startup mode of the specified activity is singletop, if it is found that the stack at the top of the return stack is already active at the start of the activity, it is considered that it can be used directly and no new activity instances will be created.</p></p><p><p>3, Singletask</p></p><p><p>When the specified active startup mode is singletask, each time the activity is started, the system checks to see if an instance of the activity exists in the return stack, uses the instance directly if it exists, and then all the activities on top of the activity are out of the stack, if no discovery is Created.</p></p><p><p>4, singleinstance</p></p><p><p>Unlike the three startup modes above, activities designated as SingleInstance mode will start a new return stack to manage this Activity. So what's the point of doing this? Imagine a scenario where we have an activity in our program that allows other programs to invoke, and if we want to implement other programs and our programs can share instances of this activity, how do we do that? Using the previous three startup modes is certainly not available, because each application will have its own return stack, and the same activity in the different return stacks is bound to create a new instance. Using the SingleInstance mode can solve this problem, in this mode there will be a separate return stack to manage the activity, regardless of which application to access the activity, the same return stack shared, also resolves the problem of shared activity Instances.</p></p><p><p>Getting Started with Android (iii) Activity-life cycle and startup mode</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service