Activity的4种启动模式

Activity4种启动模式

第一种:standard模式
Standard是Activity默认的启动模式,在不指定Activity启动模式的情况下,所有Activity使用的都是standard模式。因此,前面使用的Activity都是standard启动模式。
standard模式下,每当启动一个新的Activity,他都会进入任务栈,并处于栈顶的位置,对于使用standard模式的Activity,系统不会判断该Activity在栈中是否存在,每次启动都会创建一个新的实例。

                                    standard模式

Activity_01.xml的代码为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/ic_launcher"
        android:text="toFirst"
        android:id="@+id/btnClick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/ic_launcher"
        android:text="toFirst"
        android:id="@+id/btnClick"
/>
</LinearLayout>
Activity01.java的代码为:
package cn.edu.bzu.activitydemo;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Activity01 extends AppCompatActivity {
    private Button btnClick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_01);
        Log.d("Activity01",this.toString());
        btnClick =(Button)findViewById(R.id.btnClick);
        btnClick.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent=new Intent(Activity01.this,Activity01.class);
                startActivity(intent);
            }
        });
    }
}
 
第二种:singleTop模式
singleTop模式与standard类似,不同的是,当启动的Activity已经位于栈顶时,则直接使用它不用创建新的实例。如果启动的Activity没有位于栈顶时,则创建一个新的实例位于栈顶。

                               singleTop模式

Activity_02.xml的代码为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/ic_launcher"
        android:text="toSecand"
        android:id="@+id/btnClick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/ic_launcher"
        android:text="toSecand"
        android:id="@+id/btnClick"
/>
</LinearLayout>
Activity02.java的代码为:
package cn.edu.bzu.activitydemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Activity02 extends AppCompatActivity {
    private Button btnClick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_01);
        Log.d("Activity02",this.toString());
        btnClick =(Button)findViewById(R.id.btnClick);
        btnClick.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent=new Intent(Activity02.this,Activity01.class);
                startActivity(intent);
            }
        });
    }
}
还需要修改AndroidMainfest.xml文件,代码为:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.edu.bzu.activitydemo"
    package="cn.edu.bzu.activitydemo"
>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
>
        <activity android:name=".Activity01"
            android:launchMode="singleTop"
            android:launchMode="singleTop"
>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
 
 
第三种:singleTask模式
Activity的启动模式指定为singleTask,每次启动该Activity时,系统首先会检查栈中是否存在该Activity实例,如果发现已经存在则直接使用该实例,并将当前Activity之上的所有Activity出栈,如果没有发现则创建一个新的实例。

                  singleTask模式

需要改变Activity01.java、Activity02.java和AndroidMainfest.xml三个文件
Activity01.java的代码为:
package cn.edu.bzu.activitydemo;
        import android.app.Activity;
        import android.content.Intent;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.widget.Button;
public class Activity01 extends AppCompatActivity {
    private Button btnClick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_01);
        Log.d("Activity01",this.toString());
        btnClick =(Button)findViewById(R.id.btnClick);
        btnClick.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent=new Intent(Activity01.this,Activity01.class);
                startActivity(intent);
            }
        });
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("Activity01","onRestart()");
    }
}
Activity02.java的代码为:
package cn.edu.bzu.activitydemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Activity02 extends AppCompatActivity {
    private Button btnClick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_01);
        Log.d("Activity02",this.toString());
        btnClick =(Button)findViewById(R.id.btnClick);
        btnClick.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent=new Intent(Activity02.this,Activity01.class);
                startActivity(intent);
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("Activity02","onDestroy()");
    }
}
AndroidMainfest.xml代码为:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.edu.bzu.activitydemo"
    package="cn.edu.bzu.activitydemo"
>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
>
        <activity android:name=".Activity01"
            android:launchMode="singleTask"
            android:launchMode="singleTask"
>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
第四种:singleInstance模式
在程序开发中,如果需要Activity在整个系统中都只有一个实例,这时就需要用到singleInstance模式。不同于上述三种模式,指定为singleInstance模式的Activity会启动一个新的任务栈来管理这个Activity。
singleInstance模式加载Activity时,无论从哪个任务栈中启动Activity,指挥创建一个Activity实例,并且会使用一个新的任务栈来装载该 Activity实例。采用这种模式启动Activity会分为两种情况:
第一种:如果启动的Activity不存在,系统会先创建一个新的任务栈,再创建该Activity的实例,并把该Activity加入栈顶。
第二种:如果要启动的Activity已存在,无论位于哪个应用程序或者是哪个任务栈中,系统都会把该Activity所在的任务栈转到前台,从而使该Activity显示出来。


                                                                singleInstance模式

代码和其他三种模式的代码差不多,修改一下AndroidMainfest.xml文件就行。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值