安卓Activity的LaunchMode

Activity的启动模式

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

     自己可以启动自己

<2>SingleTop(顶单例模式)

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

     自己不能启动自己

 <3>SingleTask(内单例模式)

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

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


案例演示如下:

XML代码:

主界面main_activityxml代码:

<?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"
<?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>

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>





activity_b.xml代码

<?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>



主界面的MainActivity.java代码:

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");
    }



  
}




BActivity.java代码:

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");
    }


}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值