Android task and back stack (fallback stack)

Source: Internet
Author: User

typically an application includes multiple activity We're using the program is, a activity to another activity activity and then we press the back button and we can return it in the open order. android task and back Stack

Task A collection of pages is stored, and the collection is arranged into a set called Back Stack , the purpose of the preservation is to record Activity in the open order.

Features: The system assigns each program a unique Task and fallback stack.

Features of the fallback stack: advanced after-out .

change activity android:launchmode standard singletop singletask

1. Standard : This is the default condition.

2. Singletop: If thisActivity,Activitybetween the jumps (not from thisActivityand jump to thisActivity) is the same as the default;Activityand jump to thisActivity, new This is not createdActivity, which is theActivitydoes not reopen. When it starts, it will determine the setting of theSingletopThe page of the property is located on the stack top of the fallback stack, if it is, do not open a new page, select the previous page, if not, open the new one.

3. Singletask: When fromactivity1Open a newActivity2New in the fallback stackActivity2object is added to the stack when restartedactivity1to determine if the previous fallback stack was storedactivity1object, if there is, then theactivity1above allActivityeject the fallback stack.

    1. 4 , singleinstance: is a single-instance . Create the activity instance in a new stack and have multiple apps share the activity instance. Once an activity instance of this pattern exists on a stack, any application that activates the activity will reuse the instance in that stack, which would have the effect of sharing an app with multiple applications, regardless of who activates the activity into the same application. This startup mode works similarly to the browsers we use, and when you access the browser in multiple programs, open the browser if the current browser is not open, or 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. The activity of "Launchmode" set to "SingleInstance" is always at the bottom of the stack and can only be instantiated once, not allowing other activity to be pressed into the task stack of the "singleinstance" activity,  That is, there is only one activity in the entire task stack.

Personally, it is difficult to understand and can use the Android program to aid understanding.

Examples are as follows:

Build an Android project: There are two activities , two layout files

Mainactivity.java

Import Android.app.activity;import android.content.intent;import Android.os.bundle;import android.view.View;public Class Mainactivity extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Settitle ("mainactivity"); Setcontentview (R.layout.activity_main);} public void OnClick (View v) {switch (V.getid ()) {r.id.button1:intent Intent = new Intent (this, mainactivity.class); StartActivity (intent); Break;case r.id.button2:intent intent2 = new Intent (this, twoactivity.class); StartActivity ( INTENT2); break;}}}

Twoactivity.java

Import Android.app.activity;import android.content.intent;import Android.os.bundle;import android.view.View;public Class Twoactivity extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Settitle ("twoactivity"); Setcontentview (r.layout.two);} public void OnClick (View v) {switch (V.getid ()) {r.id.button1:intent Intent = new Intent (this, twoactivity.class); StartActivity (intent); Break;case r.id.button2:intent intent2 = new Intent (this, mainactivity.class); StartActivity ( INTENT2); break;}}}

Activity_main.xml

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"      xmlns:tools= "Http://schemas.android.com/tools"     android:layout_width= "Match_parent"     android:layout_height= "Match_parent"     android:paddingbottom= "@ Dimen/activity_vertical_margin "    android:paddingleft=" @dimen/activity_horizontal_margin "     android:paddingright= "@dimen/activity_horizontal_margin"      android:paddingtop= "@dimen/activity_vertical_margin"     tools:context= ". Mainactivity " >    <Button         Android:id= "@+id/button1"         android:layout_width= "Wrap_content"         android:layout_height= "Wrap_content"          android:layout_alignparenttop="True"         android:layout_centerhorizontal= "true"          android:layout_margintop= "104DP"          android:onclick= "OnClick"         android:text= "@string/ziji"  / >    <button        android:id= "@+id/button2 "        android:layout_width="      "Wrap_content"     android:layout_height= "Wrap_content"          android:layout_alignleft= "@+id/button1"         android:layout_below= "@ +id/button1 "        android:layout_margintop=" 61DP "         android:onclick= "OnClick"         android : text= "@string/othER " />    </RelativeLayout> 

Two.xml

<?xml version= "1.0"  encoding= "Utf-8"? ><relativelayout xmlns:android= "http// Schemas.android.com/apk/res/android "    android:layout_width=" Match_parent "     android:layout_height= "Match_parent"  >    <Button         android:id= "@+id/button1"         android : layout_width= "wrap_content"         android:layout_height= "Wrap_ Content "        android:layout_alignparenttop=" true "         android:layout_centerhorizontal= "true"          android:layout_margintop= "104DP"         android:onclick= "OnClick "        android:text=" @string/ziji " />     <Button        android:id= "@+id/button2"         android : layout_width= "wrap_content"         android:layout_height= "Wrap_ Content "        android:layout_alignleft=" @+id/button1 "         android:layout_below= "@+id/button1"          android:layout_margintop= "61DP"         android:onclick= " OnClick "        android:text=" @string/one " /></ Relativelayout>

Add the following code to the Strings.xml:

<string name= "Other" > Go to two pages </string> <string name= "One" > go to Page </string> <string name= "Ziji" & gt; Open yourself </string>

Androidmanifest.xml modified to the following code:

<activity            android:name= " Com.zzh.day06_task. Mainactivity "            android:label=" @string/ App_name "            android:launchmode=" Singletask " >            <intent-filter >                <action  android:name= "Android.intent.action.MAIN"  />                 <category android:name= " Android.intent.category.LAUNCHER " />             </intent-filter>        </activity>         <activity  Android:name= "Com.zzh.day06_task. Twoactivity "></activity>

Androidmanifest.xml constantly modify the value of android:launchmode= "Singletask" to see the effect, auxiliary understanding.


This article is from the "JDK7 Environment Building" blog, please make sure to keep this source http://zzhhz.blog.51cto.com/7107920/1626550

Android task and back stack (fallback stack)

Related Article

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.