关于android:Android-开发入门活动的启动模式

48次阅读

共计 5517 个字符,预计需要花费 14 分钟才能阅读完成。

2.5.1 standard

默认的启动模式。零碎不关怀这个流动是否曾经在返回栈中存在,每次启动都会创立该流动的一个新的实例。

@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
    Log.d("FirstActivity", this.toString());
    setContentView(R.layout.first_layout);
    Button button1 = (Button) findViewById(R.id.button_1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 在 FirstActivity 流动中启动 FirstActivity 流动
            Intent intent = new Intent(FirstActivity.this, FirstActivity.class);
            startActivity(intent);
        }
    });
}

2.5.2 singleTop

在启动流动时如果返回栈的栈顶曾经是该流动,则间接应用,不创立新的流动实例。

# app/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activitytest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".FirstActivity"
            android:launchMode="singleTop"                            #指定流动启动模式
            android:label="This is FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

但如果该流动不是位于返回栈的栈顶,还是会创立新的流动实例。

@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
    Log.d("FirstActivity", this.toString());
    setContentView(R.layout.first_layout);
    Button button1 = (Button) findViewById(R.id.button_1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 在 FirstActivity 流动中启动 SecondActivity 流动
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    });
}
@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
        Log.d("SecondActivity", this.toString());
        setContentView(R.layout.second_layout);
        Button button2 = (Button) findViewById(R.id.button_2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在 SecondActivity 流动中启动 FirstActivity 流动,会创立一个新的 FirstActivity
                Intent intent = new Intent(SecondActivity.this, FirstActivity.class);
                startActivity(intent);
            }
        });
    }

2.5.3 singleTask

每次启动该流动时零碎会在返回栈中查看是否存在该流动的实例,如果曾经存在则间接应用该实例,并把这个流动之上的所有流动出栈,如果不存在则创立一个新的流动实例。

# app/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activitytest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".FirstActivity"
            android:launchMode="singleTask"                            #指定流动启动模式
            android:label="This is FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
    Log.d("FirstActivity", this.toString());
    setContentView(R.layout.first_layout);
    Button button1 = (Button) findViewById(R.id.button_1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 在 FirstActivity 流动中启动 SecondActivity 流动
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    });
}

@Override
protected void onRestart() {super.onRestart();
    Log.d("FirstActivity", "onRestart");
}
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
    Log.d("SecondActivity", this.toString());
    setContentView(R.layout.second_layout);
    Button button2 = (Button) findViewById(R.id.button_2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 在 SecondActivity 流动中启动 FirstActivity 流动,不会创立一个新的 FirstActivity,而是复用旧的流动,触发 FirstActivity 的 onRestart 和本流动的 onDestory
            Intent intent = new Intent(SecondActivity.this, FirstActivity.class);
            startActivity(intent);
        }
    });
}

@Override
protected void onDestory() {super.onDestory();
    Log.d("SecondActivity", "onDestory");
}

2.5.4 singleInstance

为流动实例创立独自的返回栈,该流动位于新的返回栈中,其余流动位于原返回栈中。

<activity
    android:name=".SecondActivity"
    android:launchMode="singleInstance">                            #指定流动启动模式
    <intent-filter>
        <action android:name="com.example.activitytest.ACTION_START" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="com.example.activitytest.MY_CATEGORY" />
    </intent-filter>
</activity>
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
    Log.d("FirstActivity", "Task id is" + getTaskId());
    setContentView(R.layout.first_layout);
    Button button1 = (Button) findViewById(R.id.button_1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 在 FirstActivity 流动中启动 SecondActivity 流动
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    });
}
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
    Log.d("SecondActivity", "Task id is" + getTaskId());
    setContentView(R.layout.second_layout);
    Button button2 = (Button) findViewById(R.id.button_2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 在 SecondActivity 流动中启动 ThirdActivity 流动
            Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
            startActivity(intent);
        }
    });
}
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
    Log.d("ThirdActivity", "Task id is" + getTaskId());
    setContentView(R.layout.third_layout);
}

后果是:FirstActivity 和 ThirdActivity 位于同一个返回栈中,SecondActivity 独立寄存在新的返回栈中。

流动启动程序:FirstActivity -> SecondActivity -> ThirdActivity

流动退出程序:ThirdActivity -> FirstActivity -> SecondActivity -> 程序退出。

正文完
 0