5.Android(动态设置启动模式)

1.standard标准模式

(点击翻页只会新建页面,点击结束当前活动返回之前界面)


2.singleTop(栈顶复用)

(简而言之就是最新的活动在栈顶,如果当前的活动与要跳转的活动是一样的活动就复用不新建活动)

3.singleTask(单任务模式)

(设A活动已在,再次跳转到A活动时,A活动之前的活动销毁,只保留A活动)

4.singleInstance(单实例模式)

(只能有一个活动,其他活动销毁)

或者使用FLAG_ACTIVITY_REORDER_TO_FRONT(复用已有活动)

活动1

package com.example.myapplication;

import static android.content.ContentValues.TAG;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


public class page1Activity extends AppCompatActivity implements View.OnClickListener{
    private TextView text;
    private Integer num=0;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page1);
        findViewById(R.id.next_page).setOnClickListener(this);
        findViewById(R.id.finishPage1).setOnClickListener(this);
        findViewById(R.id.cur).setOnClickListener(this);
        text = findViewById(R.id.text);
    }

    @Override
    public void onClick(View v) {

        //1.跳转界面
        if(v.getId()==R.id.next_page){

            //1.默认启动模式为Standard,每次都新建界面
            //比喻:每次点外卖都给你一份全新的,即使你桌上已经有一份一模一样的。
            Intent intent = new Intent(page1Activity.this,page2Activity.class);

            //2.singleTop(单顶模式)
            //比喻:如果外卖小哥看到你桌上已经有一份同样的,就不送了(复用);如果不一样或者压在下面,就再送一份。
//            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);


            //3.singleTask(单任务模式)
            //比喻:外卖小哥发现你家里已经有这份外卖,就把其他外卖全扔了,只留这一份。
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);


            //4.singleInstance(单实例模式)
            //比喻:这份外卖必须独占一个桌子,其他外卖不能放过来。
//            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//复用已有活动
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK); // 强制新建任务栈

            startActivity(intent);
        }


        //2.关闭当前界面 并回到上一个界面
        if(v.getId()==R.id.finishPage1){
            finish();
        }

//        //3.测试单顶模式,栈顶复用
//        if(v.getId()==R.id.cur){
//            Intent intent = new Intent(page1Activity.this,page1Activity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//            startActivity(intent);
//        }

//        //4.singleTask(单任务模式)
//        if(v.getId()==R.id.cur){
//            Intent intent = new Intent(page1Activity.this,page1Activity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            startActivity(intent);
//        }

        //5.singleInstance(单实例模式)
        if(v.getId()==R.id.cur){
            Intent intent = new Intent(page1Activity.this,page1Activity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//复用已有活动
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK); // 强制新建任务栈
            startActivity(intent);
        }
    }


    @Override
    protected void onStart(){
        super.onStart();
        Log.d(TAG,"page1:onStart()从后台返回APP时的准备动作");
    }

    @Override
    protected void onResume(){
        num++;
        text.setText("出现"+num+"次");
        super.onResume();
        Log.d(TAG,"page1:onResume()用户从其他页面返回时的数据更新");
    }

    @Override
    protected void onPause(){
        super.onPause();
        Log.d(TAG,"page1:onPause()跳转到新页面前的数据保存");
    }

    @Override
    protected void onStop(){
        super.onStop();
        Log.d(TAG,"page1:onStop()APP被切换到后台时的资源释放");
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"page1:onDestroy()用户主动退出或系统回收内存时的清理");
    }

    @Override
    protected void onRestart(){
        super.onRestart();
        Log.d(TAG,"page1:onRestart()从其他APP切换回来时的数据刷新");
    }

}

活动2

package com.example.myapplication;

import static android.content.ContentValues.TAG;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class page2Activity extends AppCompatActivity implements View.OnClickListener{
    private TextView text;
    private Integer num=0;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);
        findViewById(R.id.pre_page).setOnClickListener(this);
        findViewById(R.id.finishPage2).setOnClickListener(this);
        findViewById(R.id.cur).setOnClickListener(this);
        text = findViewById(R.id.text);
    }

    @Override
    public void onClick(View v) {

        //1.跳转界面
        if(v.getId()==R.id.pre_page){

            //1.默认启动模式为Standard,每次都新建界面
            //比喻:每次点外卖都给你一份全新的,即使你桌上已经有一份一模一样的。
            Intent intent = new Intent(page2Activity.this,page1Activity.class);

            //2.singleTop(单顶模式)
            //比喻:如果外卖小哥看到你桌上已经有一份同样的,就不送了(复用);如果不一样或者压在下面,就再送一份。
//            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);


            //3.singleTask(单任务模式)
            //比喻:外卖小哥发现你家里已经有这份外卖,就把其他外卖全扔了,只留这一份。
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);


            //4.singleInstance(单实例模式)
            //比喻:这份外卖必须独占一个桌子,其他外卖不能放过来。
//            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//复用已有活动
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK); // 强制新建任务栈



            startActivity(intent);
        }

        //2.关闭当前界面 并回到上一个界面
        if(v.getId()==R.id.finishPage2){
            finish();
        }

//        //3.测试单顶模式,栈顶复用
//        if(v.getId()==R.id.cur){
//            Intent intent = new Intent(page2Activity.this,page2Activity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//            startActivity(intent);
//        }

//        //4.singleTask(单任务模式)
//        if(v.getId()==R.id.cur){
//            Intent intent = new Intent(page2Activity.this,page2Activity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            startActivity(intent);
//        }

        //5.singleInstance(单实例模式)
        if(v.getId()==R.id.cur){
            Intent intent = new Intent(page2Activity.this,page2Activity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//复用已有活动
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK); // 强制新建任务栈
            startActivity(intent);
        }



    }

    @Override
    protected void onStart(){
        super.onStart();
        Log.d(TAG,"page2:onStart()从后台返回APP时的准备动作");
    }

    @Override
    protected void onResume(){
        num++;
        text.setText("出现"+num+"次");
        super.onResume();
        Log.d(TAG,"page2:onResume()用户从其他页面返回时的数据更新");
    }

    @Override
    protected void onPause(){
        super.onPause();
        Log.d(TAG,"page2:onPause()跳转到新页面前的数据保存");
    }

    @Override
    protected void onStop(){
        super.onStop();
        Log.d(TAG,"page2:onStop()APP被切换到后台时的资源释放");
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"page2:onDestroy()用户主动退出或系统回收内存时的清理");
    }

    @Override
    protected void onRestart(){
        super.onRestart();
        Log.d(TAG,"page2:onRestart()从其他APP切换回来时的数据刷新");
    }
}

布局1

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:launchMode="singleTask"
    tools:context=".page1Activity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/next_page"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="下一页" />

        <Button
            android:id="@+id/finishPage1"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="结束当前活动" />
        <Button
            android:id="@+id/cur"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="这一页"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="60dp"
            android:id="@+id/text"
            android:text=""/>


    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

布局2

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:launchMode="singleTask"
    tools:context=".page1Activity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="上一页"
            android:id="@+id/pre_page"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="结束当前活动"
            android:id="@+id/finishPage2"/>
        <Button
            android:id="@+id/cur"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="这一页"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="60dp"
            android:id="@+id/text"
            android:text=""/>


    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值