安卓Activity的LaunchMode

来源:互联网 发布:模拟视频信号传输网络 编辑:程序博客网 时间:2024/05/19 11:46

Activity的启动模式

<1>Standard(默认-标准模式)

     自己可以启动自己

<2>SingleTop(顶单例模式)

      在栈顶只允许有一个相同的Activity

     自己不能启动自己

 <3>SingleTask(内单例模式)

     如果要启动的Activity存在,则干掉要启动的Activity上面的所有的Activity

<4>SingleInstance(全局单例模式)

案例演示如下:

XML代码:

主界面main_activityxml代码:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical"  
  8.     tools:context="com.example.cookie.android0617closeapplication.MainActivity">  
  9.     <TextView  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/tv_main_text"  
  13.         android:text="这是主界面" />  
  14.   
  15.     <Button  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:onClick="jumpSelf"  
  19.         android:text="跳自己"/>  
  20.   
  21.     <Button  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"<pre code_snippet_id="2450762" snippet_file_name="blog_20170617_1_2664347" name="code" class="html"><?xml version="1.0" encoding="utf-8"?>  
  24. <LinearLayout  
  25.     xmlns:android="http://schemas.android.com/apk/res/android"  
  26.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  27.     xmlns:tools="http://schemas.android.com/tools"  
  28.     android:layout_width="match_parent"  
  29.     android:layout_height="match_parent"  
  30.     android:orientation="vertical"  
  31.     tools:context="com.example.cookie.android0617closeapplication.BActivity">  
  32.   
  33.     <TextView  
  34.         android:layout_width="match_parent"  
  35.         android:layout_height="wrap_content"  
  36.         android:text="这是B界面累"  
  37.         android:textSize="35sp"  
  38.         android:background="#6600"  
  39.         />  
  40.     <TextView  
  41.         android:layout_width="match_parent"  
  42.         android:layout_height="wrap_content"  
  43.         android:id="@+id/tb_b_word"/>  
  44.   
  45.     <Button  
  46.         android:layout_width="match_parent"  
  47.         android:layout_height="wrap_content"  
  48.         android:onClick="jumpMain"  
  49.         android:text="跳到主页面"/>  
  50.   
  51.     <Button  
  52.         android:layout_width="match_parent"  
  53.         android:layout_height="wrap_content"  
  54.         android:onClick="exit"  
  55.         android:text="退出应用程序"/>  
  56.   
  57. </LinearLayout>  
  58. </pre><br>  
  59. android:layout_margin="10dp" android:onClick="jumpOther" android:text="跳别人"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="退出应用程序" android:onClick="exit"/></LinearLayout><p></p>  
  60. <pre></pre>  
  61. <p></p>  
  62. <p><span style="font-size:14px"><strong><br>  
  63. </strong></span></p>  
  64. <p><span style="font-size:14px"><strong><br>  
  65. </strong></span></p>  
  66. <p><span style="font-size:14px"><strong><br>  
  67. </strong></span></p>  
  68. <p><span style="font-size:14px"><strong>activity_b.xml代码</strong></span></p>  
  69. <p><span style="font-size:14px"></span></p><pre code_snippet_id="2450762" snippet_file_name="blog_20170617_2_7364429" name="code" class="html"><?xml version="1.0" encoding="utf-8"?>  
  70. <LinearLayout  
  71.     xmlns:android="http://schemas.android.com/apk/res/android"  
  72.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  73.     xmlns:tools="http://schemas.android.com/tools"  
  74.     android:layout_width="match_parent"  
  75.     android:layout_height="match_parent"  
  76.     android:orientation="vertical"  
  77.     tools:context="com.example.cookie.android0617closeapplication.BActivity">  
  78.   
  79.     <TextView  
  80.         android:layout_width="match_parent"  
  81.         android:layout_height="wrap_content"  
  82.         android:text="这是B界面累"  
  83.         android:textSize="35sp"  
  84.         android:background="#6600"  
  85.         />  
  86.     <TextView  
  87.         android:layout_width="match_parent"  
  88.         android:layout_height="wrap_content"  
  89.         android:id="@+id/tb_b_word"/>  
  90.   
  91.     <Button  
  92.         android:layout_width="match_parent"  
  93.         android:layout_height="wrap_content"  
  94.         android:onClick="jumpMain"  
  95.         android:text="跳到主页面"/>  
  96.   
  97.     <Button  
  98.         android:layout_width="match_parent"  
  99.         android:layout_height="wrap_content"  
  100.         android:onClick="exit"  
  101.         android:text="退出应用程序"/>  
  102.   
  103. </LinearLayout>  
  104. </pre><br>  
  105. <strong><br>  
  106. </strong><p></p>  
  107. <p><span style="font-size:14px"><strong><br>  
  108. </strong></span></p>  
  109. <p><span style="font-size:14px"><strong>主界面的MainActivity.java代码:</strong></span></p>  
  110. <p><span style="font-size:14px"></span></p><pre code_snippet_id="2450762" snippet_file_name="blog_20170617_3_254368" name="code" class="html">public class MainActivity extends Activity {  
  111.   
  112.     private TextView tv_main_text;  
  113.     private MyApplication myApplication;  
  114.     private long firstTime=0;  
  115.   
  116.     @Override  
  117.     protected void onCreate(Bundle savedInstanceState) {  
  118.         super.onCreate(savedInstanceState);  
  119.         setContentView(R.layout.activity_main);  
  120.         tv_main_text = (TextView) findViewById(R.id.tv_main_text);  
  121.         tv_main_text.setText("当前Activity实例:"+this+"\n当前任务栈"+this.getTaskId());  
  122.         //1.获取Myapplication  
  123.         myApplication = (MyApplication) getApplication();  
  124.         myApplication.add(this);  
  125.   
  126.   
  127.   
  128.     }  
  129.   
  130.     public void jumpSelf(View view){  
  131.         Intent intent=new Intent(this,MainActivity.class);  
  132.         startActivity(intent);  
  133.     }  
  134.   
  135.     public void jumpOther(View view){  
  136.         Intent intent=new Intent(this,BActivity.class);  
  137.         //startActivity(intent);  
  138.         startActivityForResult(intent,0x22);  
  139.     }  
  140.   
  141.     public void exit(View view){  
  142.         finish();//退出当前的Activity  
  143.         //finish()和System.exit(0)都是退出当前的Activity,  
  144.         //但是finish()会调用onDestroy()方法,  
  145.        // System.exit(0);  
  146.         myApplication.exit();  
  147.     }  
  148.   
  149. //    @Override  
  150. //    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  151. //        super.onActivityResult(requestCode, resultCode, data);  
  152. //        exit(null);  
  153. //    }  
  154.   
  155.     @Override  
  156.     protected void onDestroy() {  
  157.         super.onDestroy();  
  158.         Log.i("test","onDestroy");  
  159.     }  
  160.   
  161.   
  162.   
  163.     
  164. }  
  165. </pre><p></p>  
  166. <p><span style="font-size:14px"><br>  
  167. </span></p>  
  168. <p><strong><span style="font-size:14px"><br>  
  169. </span></strong></p>  
  170. <p><strong><span style="font-size:14px"><br>  
  171. </span></strong></p>  
  172. <p><strong><span style="font-size:14px">BActivity.java代码:</span></strong></p>  
  173. <p><span style="font-size:14px"></span></p><pre code_snippet_id="2450762" snippet_file_name="blog_20170617_4_3144307" name="code" class="html">public class BActivity extends AppCompatActivity {  
  174.   
  175.     private TextView tb_b_word;  
  176.     private MyApplication myApplication;  
  177.   
  178.     @Override  
  179.     protected void onCreate(Bundle savedInstanceState) {  
  180.         super.onCreate(savedInstanceState);  
  181.         setContentView(R.layout.activity_b);  
  182.         tb_b_word = (TextView) findViewById(R.id.tb_b_word);  
  183.         tb_b_word.setText("当前Activity地址:"+this+"\nTaskId:"+this.getTaskId());  
  184.         //1.获取Myapplication  
  185.         myApplication = (MyApplication) getApplication();  
  186.         myApplication.add(this);  
  187.     }  
  188.   
  189.     public void jumpMain(View view){  
  190.         Intent intent=new Intent(this,MainActivity.class);  
  191.         startActivity(intent);  
  192.   
  193.     }  
  194.   
  195.     public void exit(View view){  
  196.       // finish();  
  197.        // System.exit(0);  
  198.         myApplication.exit();  
  199.     }  
  200.   
  201.     @Override  
  202.     protected void onDestroy() {  
  203.         super.onDestroy();  
  204.         Log.i("test","onDestroy");  
  205.     }  
  206.   
  207.   
  208. }  
  209. </pre><br>  
  210. <p></p>  
  211. <p><span style="font-size:14px"><br>  
  212. </span></p>  
  213. <p><span style="font-size:14px"><br>  
  214. </span></p>  
  215. <p><span style="font-size:14px"><img src="http://img.blog.csdn.net/20170617200830674?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWURDb29raWU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""><br>  
  216. </span></p>  
  217. <p><span style="font-size:14px"><br>  
  218. </span></p>  
  219. <p><span style="font-size:14px"><br>  
  220. </span></p>  
  221. <p><span style="font-size:14px"><br>  
  222. </span></p>  
  223. <p><span style="font-size:14px"><br>  
  224. </span></p>  


原创粉丝点击