您的位置:首页 > 其它

Activity启动模式之SingleTask

2016-11-17 20:48 155 查看
  对于SingleTop模式,即使栈中存在活动的实例,但是如果不在栈顶的位置,系统还是会创建该活动的情况下。如果将启动模式设置为SingleTask模式,那么在启动该活动时,系统检测到栈中存在该活动的实例,就不会再创建该活动了,而只是会将处于该活动上方的所有活动都出栈,使得该活动处于栈顶位置。
  同样的,贴上代码来检测一下。创建项目LearnLaunchMode_SingleTask,其各部分代码如下:
FirstActivity.java:

1 public class FirstActivity extends ActionBarActivity {
2     private Button launch_second, launch_third;
3     @Override
4     protected void onCreate(Bundle savedInstanceState) {
5         super.onCreate(savedInstanceState);
6         setContentView(R.layout.activity_first);
7
8         launch_second = (Button) findViewById(R.id.launch_second);
9         launch_third = (Button) findViewById(R.id.launch_third);
10         launch_second.setOnClickListener(new View.OnClickListener() {
11             @Override
12             public void onClick(View v) {
13                 Intent intent = new Intent().setClass(FirstActivity.this, SecondActivity.class);
14                 startActivity(intent);
15             }
16         });
17         launch_third.setOnClickListener(new View.OnClickListener() {
18             @Override
19             public void onClick(View v) {
20                 Intent intent = new Intent().setClass(FirstActivity.this, ThirdActivity.class);
21                 startActivity(intent);
22             }
23         });
24         Log.i("TAG", this + " is launch");
25     }
26
27     @Override
28     protected void onDestroy() {
29         super.onDestroy();
30         Log.i("TAG", this + " is destroy!");
31     }
32 }


SecondActivity.java:

1 public class SecondActivity extends Activity {
2     private Button launch_first, launch_third;
3     @Override
4     protected void onCreate(Bundle savedInstanceState) {
5         super.onCreate(savedInstanceState);
6         requestWindowFeature(Window.FEATURE_NO_TITLE);
7         setContentView(R.layout.activity_second);
8
9         launch_first = (Button) findViewById(R.id.launch_first);
10         launch_third = (Button) findViewById(R.id.launch_third);
11         launch_first.setOnClickListener(new View.OnClickListener() {
12             @Override
13             public void onClick(View v) {
14                 Intent intent = new Intent().setClass(SecondActivity.this, FirstActivity.class);
15                 startActivity(intent);
16             }
17         });
18         launch_third.setOnClickListener(new View.OnClickListener() {
19             @Override
20             public void onClick(View v) {
21                 Intent intent = new Intent().setClass(SecondActivity.this, ThirdActivity.class);
22                 startActivity(intent);
23             }
24         });
25         Log.i("TAG", this + " is launch");
26     }
27
28     @Override
29     protected void onDestroy() {
30         super.onDestroy();
31         Log.i("TAG", this + " is destroy!");
32     }
33 }


ThirdActivity.java:

1 public class ThirdActivity extends Activity {
2     private Button launch_first, launch_second;
3     @Override
4     protected void onCreate(Bundle savedInstanceState) {
5         super.onCreate(savedInstanceState);
6         requestWindowFeature(Window.FEATURE_NO_TITLE);
7         setContentView(R.layout.activity_third);
8
9         launch_first = (Button) findViewById(R.id.launch_first);
10         launch_second = (Button) findViewById(R.id.launch_second);
11         launch_first.setOnClickListener(new View.OnClickListener() {
12             @Override
13             public void onClick(View v) {
14                 Intent intent = new Intent().setClass(ThirdActivity.this, FirstActivity.class);
15                 startActivity(intent);
16             }
17         });
18         launch_second.setOnClickListener(new View.OnClickListener() {
19             @Override
20             public void onClick(View v) {
21                 Intent intent = new Intent().setClass(ThirdActivity.this, SecondActivity.class);
22                 startActivity(intent);
23             }
24         });
25         Log.i("TAG", this + " is launch");
26     }
27
28     @Override
29     protected void onDestroy() {
30         super.onDestroy();
31         Log.i("TAG", this + " is destroy!");
32     }
33 }


activity_first.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3     xmlns:android="http://schemas.android.com/apk/res/android"
4     android:layout_width="match_parent"
5     android:layout_height="match_parent"
6     android:orientation="vertical">
7     <Button
8         android:id="@+id/launch_second"
9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:text="启动第二个Activity!" />
12     <Button
13         android:id="@+id/launch_third"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="启动第三个Activity!" />
17 </LinearLayout>


activity_second.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3     xmlns:android="http://schemas.android.com/apk/res/android"
4     android:layout_width="match_parent"
5     android:layout_height="match_parent"
6     android:orientation="vertical">
7     <Button
8         android:id="@+id/launch_first"
9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:text="启动第一个Activity!" />
12     <Button
13         android:id="@+id/launch_third"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="启动第三个Activity!" />
17 </LinearLayout>


activity_third.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3     xmlns:android="http://schemas.android.com/apk/res/android"
4     android:layout_width="match_parent"
5     android:layout_height="match_parent"
6     android:orientation="vertical">
7     <Button
8         android:id="@+id/launch_first"
9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:text="启动第一个Activity!" />
12     <Button
13         android:id="@+id/launch_second"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="启动第二个Activity!" />
17 </LinearLayout>


  同样,还需要在AndroidManifest.java中对SecondActivity以及ThirdActivity两个活动进行注册,并设置上述三个活动的启动模式为SingleTask:

1 <activity
2     android:name=".FirstActivity"
3     android:launchMode="singleTask"
4     android:label="@string/app_name">
5     <intent-filter>
6         <action android:name="android.intent.action.MAIN" />
7         <category android:name="android.intent.category.LAUNCHER" />
8     </intent-filter>
9 </activity>
10 <activity android:name=".SecondActivity" android:launchMode="singleTask" />
11 <activity android:name=".ThirdActivity" android:launchMode="singleTask" />


  在该程序中,系统先启动FirstActivity,此时打印信息显示该活动启动了;然后我们点击"启动第二个Activity!"按钮,可以看到显示信息中SecondActivity is launch;再点击"启动第三个Activity!",也可以看到第三个活动启动了。最后再点击"启动第一个Activity!"。从时观察打印信息可以看到;SecondActivity is destroy;以及ThirdActivity is destroy。表示此时SecondActivity与ThirdActivity都已出栈。现在只需要按一次back键即可退出程序。其他的各种创建活动的顺序就不再做演示了。


 
  综上所述,可以画出该启动模式的原理示意图如下:



 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐