Four startup modes in Android activity

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">The startup mode of Android total activity is divided into four types:</span><br></p></p><pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;"><activity android:name= ". Mainactivity "android:launchmode=" Standard "/></pre></pre><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px;">The<strong></strong>"standard" <span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; line-height: 26px; background-color: rgb(255, 255, 255);">default</span> mode startup mode, each time the activity is activated, will be created and put into the task Stack.</span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px;">"<strong>singletop</strong>" If there is an instance of the activity at the top of the task, reusing the instance will create a new instance and put it on top of the stack (even if the activity instance already exists on the stack, as long as it is not on top of the stack).</span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px;">"<strong>singletask</strong>" If there is already an instance of the activity in the stack, reuse the instance (the onnewintent () of the instance is invoked). When reused, the instance is returned to the top of the stack, so the instances above it will be removed from the STACK. If the instance does not exist in the stack, a new instance is created to be put into the STACK.</span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px;">"<strong>singleinstance</strong>" creates the activity instance in a new stack and lets multiple apps share the activity instance in the Stack. Once an instance of the activity in the schema is present on a stack, any application that activates the activity will reuse the instance in that stack, with the effect of sharing an app with multiple applications, regardless of who activates the activity into the same application.</span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; line-height: 26px; background-color: rgb(255, 255, 255);">1.standard:</span></strong></span></p></p><pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;"><activity android:name= ". Mainactivity "android:launchmode=" Standard "/></pre></pre><pre class="brush:java;toolbar: true; auto-links: false;">Public class mainactivity extends actionbaractivity {    private  button btn;    private textview text_show;    @ Override    protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         setcontentview (r.layout.activity_main);text_show =  (TextView)  findViewById ( r.id.text_show); text_show.settext (this.tostring ());btn =  (Button) Findviewbyid (r.id.btn); Btn.setonclicklistener (new onclicklistener ()  {     @Override      public void onclick (view v)  {         Launchstandard (v);     });    }         //button Click event        public void launchstandard (view v) {       StartActivity (new intent (this,mainactivity.class));       text_show.settext ( This.tostring ());       }  }</pre><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">The initialization interface is as Follows:</span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"></span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">When the button is clicked, a new activity is created, which can be seen by the display of the 16-digit number after [email protected], and two times the interface is as Follows:</span></p></p><p><p><span style="font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"></span></p></p><p><p></p></p><p><p></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px;">At this point, we analyze the operating mechanism inside the Stack: (from the top of the stack in Turn)</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px;"></span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">therefore, This standard mode creates a new activity object each time, and when you click the back button, he destroys the top of the stack (the current Activity) and jumps to the next level, for example, if the activity is 44ED8C50 now, Then when we click back, the activity changes to 44f28a48, but when you click the button again in this activity to create the object, it creates a new activity object, which is probably not what we need in most cases, because it consumes too much of the System's performance.</span><br></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; line-height: 26px; background-color: rgb(255, 255, 255);">2.<span style="font-family: Arial; line-height: 26px; background-color: rgb(255, 255, 255);">singletop</span></span></strong></span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">The current activity at the top of the stack is automatically detected each time a new activity is used, whether it is an activity that needs to be referenced, or if it is directly referenced, without creating a new Activity.</span></p></p><p><p><span style="font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">We added a "start singletop mode" button in the interface just now, when we clicked, we created the singletop, there was a button in activity singletop to start the singletop mode, which means to start the current activity, Since we configured the Activity's startup mode in the manifest file to be singletop, we will not create this at this time but take advantage of the singletopactivity at the top of the current stack:</span></p></p><pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;"><activity android:name= ". Singletopactivity "android:launchmode=" singletop "/></pre></pre><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"></span><span style="font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">Interface Initialization:</span></p></p><p><p><span style="font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"></span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">Click on the "start singletop mode" button:</span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"></span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">We analyze its operating mechanism, we know that when the program runs to this point, the data form in the stack is:</span></p></p><p><p><span style="font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"></span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">When we click on the "start singletop mode" button in the upper interface, because this activity set startup mode is singletop, so it first detects whether the current stack top is the activity object that we want to request, has been verified, So it does not create new activity, but refers to the activity at the top of the current Stack.</span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"></span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">Although it does not create a new activity object, it uses the Onnewintent () method for each callback:</span></p></p><pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;">@Overrideprotected void onnewintent (Intent Intent) {//TODO auto-generated method stub super.onnewintent (Intent); Toast.maketext (this, New Date (). toString (), 1). show ();</pre></pre><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">We write the code for this method to output the current date, and the current date is output each time you click the button Above.</span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 18px;"><strong><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; line-height: 26px; background-color: rgb(255, 255, 255);">3.<span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; line-height: 26px; background-color: rgb(255, 255, 255);">Singletask</span></span></strong></span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">This startup mode and Singletop in the name can see the difference, that is, singletop only detect the current stack at the top of the activity is we need to request to create, and Singletask will detect the stack of all activity objects, from the top down, If it detects what we are requesting, it will destroy the object above the activity object and put the detected activity to the top of the stack directly.</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘;">We create a singletaskactivity, which contains a launch mainactivity and launch Singletaskactivity Button.</span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">Interface Initialization:</span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"></span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">Click on the "start singletask mode" button:</span></p></p><p><p></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">Click on the second button "launch singletask mode" button in this interface, according to the definition will detect whether the activity object in the current stack, so the current activity is displayed, will not be recreated;</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘;">Then click on the "start standard mode" button, since Mainactivity's boot mode is standard, a Mainactivity object will be recreated here:</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; font-size: 14px; line-height: 26px; white-space: normal; background-color: rgb(255, 255, 255);"><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘;"></span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">The data format in this stack is:</span><br></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"></span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">When you click on the "start singletask mode" button in the upper interface, we will destroy the topmost mainactivity, and then set the singletaskactivity to the top of the stack, because the second activity we are trying to create is detected in the current Stack.</span></p></p><p><p><span style="font-size: 18px;"><strong><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; line-height: 26px; background-color: rgb(255, 255, 255);">4.<span style="font-family: Arial; line-height: 26px; background-color: rgb(255, 255, 255);">SingleInstance</span></span></strong></span></p></p><p><p><span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">This startup mode works similarly to the browsers we use, and we all know that when accessing a browser in multiple programs, if the current browser is not open, the browser opens, otherwise it will be accessed in the currently open Browser. This mode saves a lot of system resources because he can guarantee that the activity object to be requested only exists in the current STACK. <span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 22.5px; color: rgb(51, 51, 51); letter-spacing: 0.5px; background-color: rgb(255, 255, 255);">when the pattern</span> <span style="font-family: 微软雅黑, ‘Microsoft YaHei‘; font-size: 14px; line-height: 22.5px; padding: 0px; margin: 0px; color: rgb(51, 51, 51); letter-spacing: 0.5px; background-color: rgb(255, 255, 255);">Once the activity instance is created in the task stack, the activity is reused by invoking the Newinstance () method of the instance as long as the instance is still in the task stack, that is, as long as the activity of that type is Activated. The same activity instance is used at this point, and it will be at the top of the stack of the task Stack. </span></span></p></p><p><p>Four startup modes in Android activity</p></p></span>

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.