This example shows how to change the order of existing activity in the current task stack by setting the intent object's markup.
1. Activity initiation Tag description for intent object:
Flag_activity_brought_to_front
This tag is typically not set in the application code, but rather by the system's settings for the activity that initiates the mode of a single task.
If you add this tag to a intent object, any remaining tasks associated with the activity will be purged before it is started. This means that the new activity will be the root of an empty task, and that any others will be destroyed. It follows the Flag_activity_new_task joint use.
If you set this tag on a intent object and the activity that you want to start is already running in the current task, then instead of creating a new instance of the event, you turn off all the other active objects on the stack. The new intent object is then sent to the surviving activity (at the top of the stack).
Flag_activity_clear_when_task_rest
If this tag is set for the intent object, the activity after the mark point in the task's activity stack should be cleared when the task is reset.
Flag_activity_exclude_from_recents
If you set this tag on a intent object, the new activity will not be kept in the list of recently launched activity.
Flag_activity_forward_result
If this tag is set on the intent object, and the intent object is used to start a new activity from a surviving activity, then the recovery target of the existing activity is transferred to the new activity. New activity obtained in this way can call the Setresult (int) method and return the result to the original activity.
Flag_activity_launched_from_history
This tag is usually not set by the application code, and if the activity is started in history, the system will set this tag.
Flag_activity_multiple_task
This tag is not used unless you implement your own top-level application launcher.
If you set this tag on a intent object, on the history stack, this activity will be the starting point for a new task.
Flag_activity_no_animation
If this tag is set on the intent object, it will prevent the system from switching between the activity animations.
If you set this tag on a intent object, the new activity will not be kept in the history stack.
Flag_activity_no_user_action
If you set this tag on a intent object, it blocks the callback of the normal Onuserleavehint () method until the activity that is newly initiated into the foreground is suspended. If the phone dials or alarm clock program uses this tag to start the activity.
Flag_activity_previous_is_top
If you set this tag on a intent object and the intent object is used to start a new activity from a surviving activity, the activity cannot be used to accept the new intent object that is sent to the top-level activity. It is generally assumed that an activity that starts with this tag is terminated immediately by itself.
Flag_activity_reorder_to_front
If this tag is set on the intent object, it will cause the surviving activity in the task history stack to be brought to the foreground.
flag_activity_reset_task_if_needed
If this tag is set for the intent object, and the activity is started in a new task, it can also be taken to the top level in the existing task stack, and it will be started as the front door of the task.
If this tag is set for the intent object, the activity will not be started if the activity to be started is already running at the top of the history stack.
Flag_activity_task_on_home
If you set this tag on a intent object, it causes the newly initiated task to be placed on top of the current master activity task.
2. Sample Code
2.1. Define the manifest file (androidmanifest.xml)
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
package=" my.android.test "
android:versioncode=" 1 "
android:versionname=" 1.0 ">
< Application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name=
". Reorderonlaunch "
android:label=" @string/app_name ">
<intent-filter>
<action android:name = "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name= ". Reordertwo "/>
<activity android:name=". Reorderthree "/>
<activity android:name=". Reorderfour "/>
</application>
<uses-sdk android:minsdkversion=" 9 "/>
</manifest >
2.2. Define string resources (Strings.xml)
<?xml version= "1.0" encoding= "Utf-8"?> <resources> <string name= "Hello" >hello world, reorderonlaunch!</string> <string name= "app_name" >ReorderOnLaunch</string> <string name= " Reorder_on_launch ">this is the" a sequence of four activities. A button on the fourth would use the Intent.flag_activity_reorder_to_front FLAG to bring the second of the activities to th E Front of the history stack. After this, proceeding back through the history should to begin with the newly-frontmost second activity, reorder the FO Urth, the third, and finally the first.</string> <string name= "Reorder_launch_two" >go to the Second</strin g> <string name= "Reorder_two_text" >this is the second in a sequence of four activities.</string> Name= "Reorder_launch_three" >go to the third</string> <string name= "Reorder_three_text" >This is the Third of a sequence of four activities.</string> <string name= "Reorder_Launch_four ">go to the fourth</string> <string name=" Reorder_four_text ">this are the last in a sequence of f
Our activities.</string> <string name= "Reorder_second_to_front" >bring the second in front</string> </resources>
2.3. Define Layout files
Reorder_on_launch.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:padding=" 4dip "
android:gravity=" Center_ Horizontal "
android:layout_width=" match_parent "
android:layout_height=" match_parent ">
< TextView
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:layout _weight= "0"
android:paddingbottom= "4dip"
android:text= "@string/reorder_on_launch"/>
< Button android:id= "@+id/reorder_launch_two"
android:layout_width= "wrap_content"
android:layout_height = "Wrap_content"
android:text= "@string/reorder_launch_two"/>
</LinearLayout>
Reorder_two.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:padding=" 4dip "
android:gravity=" Center_ Horizontal "
android:layout_width=" match_parent "
android:layout_height=" match_parent ">
< TextView
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:layout_ weight= "0"
android:paddingbottom= "4dip"
android:text= "@string/reorder_two_text"/>
<button
android:id= "@+id/reorder_launch_three"
android:layout_width= "wrap_content"
android:layout_ height= "Wrap_content"
android:text= "@string/reorder_launch_three"/>
</LinearLayout>
Reorder_three.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:padding=" 4dip "
android:gravity=" Center_ Horizontal "
android:layout_width=" match_parent "
android:layout_height=" match_parent ">
< TextView
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:layout_ weight= "0"
android:paddingbottom= "4dip"
android:text= "@string/reorder_three_text"/>
< Button android:id= "@+id/reorder_launch_four"
android:layout_width= "wrap_content"
android:layout_ height= "Wrap_content"
android:text= "@string/reorder_launch_four"/>
</LinearLayout>
Reorder_four.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:padding=" 4dip "
android:gravity=" Center_ Horizontal "
android:layout_width=" match_parent "
android:layout_height=" match_parent ">
< TextView
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:layout_ weight= "0"
android:paddingbottom= "4dip"
android:text= "@string/reorder_four_text"/>
< Button android:id= "@+id/reorder_second_to_front"
android:layout_width= "wrap_content"
android:layout_ height= "Wrap_content"
android:text= "@string/reorder_second_to_front"/>
</LinearLayout>
2.4. Create an activity
Reorderonlaunch.java
Package my.android.test;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
public class Reorderonlaunch extends activity {
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.reorder_on_launch);
Button Twobutton = (button) Findviewbyid (r.id.reorder_launch_two);
Twobutton.setonclicklistener (Mclicklistener);
}
Private final Onclicklistener Mclicklistener = new Onclicklistener () {public
void OnClick (View v) {
StartActivity (New Intent (Reorderonlaunch.this, Reordertwo.class));
}
Reordertwo.java
Package my.android.test;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
public class Reordertwo extends activity {
@Override
protected void onCreate (Bundle savestate) {
Super.oncreate (savestate);
Setcontentview (r.layout.reorder_two);
Button Twobutton = (button) Findviewbyid (r.id.reorder_launch_three);
Twobutton.setonclicklistener (Mclicklistener);
}
Private final Onclicklistener Mclicklistener = new Onclicklistener () {
publicvoid OnClick (View v) {
StartActivity (New Intent (Reordertwo.this, Reorderthree.class));
}
Reorderthree.java
Package my.android.test;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
public class Reorderthree extends activity {
private final onclicklistener Mclicklistener = new Onclicklistener () {
public void OnClick (View v) {
startactivity (new Intent (Reorderthree.this, Reorderfour.class));
}
;
@Override
protected void onCreate (Bundle savestate) {
super.oncreate (savestate);
Setcontentview (r.layout.reorder_three);
Button Twobutton = (button) Findviewbyid (r.id.reorder_launch_four);
Twobutton.setonclicklistener (Mclicklistener);
}
Reorderfour.java
Package my.android.test;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Publicclass Reorderfour extends Activity {
@Override
protected void onCreate (Bundle savestate) {
Super.oncreate (savestate);
Setcontentview (r.layout.reorder_four);
Button Twobutton = (button) Findviewbyid (R.id.reorder_second_to_front);
Twobutton.setonclicklistener (Mclicklistener);
}
Private final Onclicklistener Mclicklistener = new Onclicklistener () {public
void OnClick (View v) {
Intent Intent = new Intent (reorderfour.this, reordertwo.class);
Intent.addflags (Intent.flag_activity_reorder_to_front);
StartActivity (intent);}}
3.activity Stack Management class
Package Net.oschina.app;
Import Java.util.Stack;
Import android.app.Activity;
Import Android.content.Context; /** * Activity Stack Management/public class AppManager {private static stack<activity> activitystack; private static Appman
Ager instance; Private AppManager () {}/** * Single instance */public static AppManager Getappmanager () {if (instance = null) {instance = new Ap
Pmanager ();
return instance; /** * Add activity to stack/public void addactivity (activity activity) {if (Activitystack = null) {Activitystack = new Sta
Ck<activity> ();
} activitystack.add (activity); /** * Get Current activity (last pressed in stack)/public activity currentactivity () {Active activity = activitystack.lastelement (); r
Eturn activity; /** * Ends the current activity (last pressed in stack)/public void finishactivity () {Active activity = activitystack.lastelement (); finish
Activity (activity); }/** * Ends the specified activity/public void finishactivity (activity activity) {if (Activity!= null &&!activity.isfinis Hing ()) {ActivitystacK.remove (activity);
Activity.finish ();
activity = NULL; }/** * Ends the activity of the specified class name/public void finishactivity (class<?> cls) {to (activity Activity:activitystack) {if (Activity.getclass (). Equals (CLS))
{finishactivity (activity); }/** * End all activity/public void finishallactivity () {for (int i = 0, size = activitystack.size (); i < size; i++)
{if (null!= activitystack.get (i)) {finishactivity (Activitystack.get (i)); break;} activitystack.clear (); /** * Gets the specified activity/public static activity Getactivity (Class<?> CLS) {if (activitystack!= null) for the activity
Activity:activitystack) {if (Activity.getclass (). Equals (CLS)) {return activity;} return null; /** * Exit application/public void AppExit {try {finishallactivity ()//Kill the application process Android.os.Process.killProc
ESS (Android.os.Process.myPid ());
System.exit (0); The catch (Exception e) {}}}
The above is a small set to introduce the Android activity stack and management examples, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!