Android_LaunchMode
来源:互联网 发布:nginx centos yum 编辑:程序博客网 时间:2024/06/17 02:31
1.Activity的启动模式 Fragment
<1>Standard(默认-标准模式)
自己可以启动自己(会创新一个新的Activity)
<2>SingleTop(顶单例模式)
在栈顶只允许有一个相同的Activity
自己不能启动自己
<3>SingleTask(内单例模式)
如果要启动的Activity存在,则干掉要启动的Activity上面的所有的Activity
<4>SingleInstance(全局单例模式)
案列代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.cookie.android0617closeapplication.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_main_text" android:text="这是主界面" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="jumpSelf" android:text="跳自己"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content"<pre class="html" name="code" snippet_file_name="blog_20170617_1_2664347" code_snippet_id="2450762"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.cookie.android0617closeapplication.BActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是B界面累" android:textSize="35sp" android:background="#6600" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tb_b_word"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="jumpMain" android:text="跳到主页面"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="exit" android:text="退出应用程序"/> </LinearLayout> </pre><br> 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> <pre></pre> <p></p> <p><span style="font-size:14px"><strong><br> </strong></span></p> <p><span style="font-size:14px"><strong><br> </strong></span></p> <p><span style="font-size:14px"><strong><br> </strong></span></p> <p><span style="font-size:14px"><strong>activity_b.xml代码</strong></span></p> <p><span style="font-size:14px"></span></p><pre class="html" name="code" snippet_file_name="blog_20170617_2_7364429" code_snippet_id="2450762"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.cookie.android0617closeapplication.BActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是B界面累" android:textSize="35sp" android:background="#6600" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tb_b_word"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="jumpMain" android:text="跳到主页面"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="exit" android:text="退出应用程序"/> </LinearLayout> </pre><br> <strong><br> </strong><p></p> <p><span style="font-size:14px"><strong><br> </strong></span></p> <p><span style="font-size:14px"><strong>主界面的MainActivity.<a title="Java 知识库" class="replace_word" style="color:#df3434; font-weight:bold;" href="http://lib.csdn.net/base/java" target="_blank">Java</a>代码:</strong></span></p> <p><span style="font-size:14px"></span></p><pre class="html" name="code" snippet_file_name="blog_20170617_3_254368" code_snippet_id="2450762">public class MainActivity extends Activity { private TextView tv_main_text; private MyApplication myApplication; private long firstTime=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_main_text = (TextView) findViewById(R.id.tv_main_text); tv_main_text.setText("当前Activity实例:"+this+"\n当前任务栈"+this.getTaskId()); //1.获取Myapplication myApplication = (MyApplication) getApplication(); myApplication.add(this); } public void jumpSelf(View view){ Intent intent=new Intent(this,MainActivity.class); startActivity(intent); } public void jumpOther(View view){ Intent intent=new Intent(this,BActivity.class); //startActivity(intent); startActivityForResult(intent,0x22); } public void exit(View view){ finish();//退出当前的Activity //finish()和System.exit(0)都是退出当前的Activity, //但是finish()会调用onDestroy()方法, // System.exit(0); myApplication.exit(); } // @Override // protected void onActivityResult(int requestCode, int resultCode, Intent data) { // super.onActivityResult(requestCode, resultCode, data); // exit(null); // } @Override protected void onDestroy() { super.onDestroy(); Log.i("test","onDestroy"); } } </pre><p></p> <p><span style="font-size:14px"><br> </span></p> <p><strong><span style="font-size:14px"><br> </span></strong></p> <p><strong><span style="font-size:14px"><br> </span></strong></p> <p><strong><span style="font-size:14px">BActivity.java代码:</span></strong></p> <p><span style="font-size:14px"></span></p><pre class="html" name="code" snippet_file_name="blog_20170617_4_3144307" code_snippet_id="2450762">public class BActivity extends AppCompatActivity { private TextView tb_b_word; private MyApplication myApplication; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); tb_b_word = (TextView) findViewById(R.id.tb_b_word); tb_b_word.setText("当前Activity地址:"+this+"\nTaskId:"+this.getTaskId()); //1.获取Myapplication myApplication = (MyApplication) getApplication(); myApplication.add(this); } public void jumpMain(View view){ Intent intent=new Intent(this,MainActivity.class); startActivity(intent); } public void exit(View view){ // finish(); // System.exit(0); myApplication.exit(); } @Override protected void onDestroy() { super.onDestroy(); Log.i("test","onDestroy"); } } </pre><br> <p></p> <p><span style="font-size:14px"><br> </span></p> <p><span style="font-size:14px"><br> </span></p> <p><span style="font-size:14px"><img alt="" src="http://img.blog.csdn.net/20170617200830674?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWURDb29raWU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"><br> </span></p> <p><span style="font-size:14px"><br> </span></p> <p><span style="font-size:14px"><br> </span></p> <p><span style="font-size:14px"><br> </span></p> <p><span style="font-size:14px"><br> </span></p>