(一)Activity标准启动模式(Activity默认的standard启动模式)
1)Activity的任务棧机制:
同一个Activity中的任务棧Id是一样的,只是实例化不同而已;
比如:
在mainfest.xml中的模式设置如下:(系统默认模式,不需要android:launchMode=”standard”进行配置)
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
**android:launchMode="standard">**\\看这里,可以设置Activity的启动模式饿;
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.java如下:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button_1);
textView = (TextView)findViewById(R.id.myTextView);
textView.setText(String.format("任务棧ID =%d \n 当前Activity的实例Id = \n %s", getTaskId(), toString()));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//启动Activity自身;
startActivity(new Intent(MainActivity.this ,MainActivity.class));
}
});
}
}
执行结果:
点击Button后的结果:
可以看到点击button后,Activity的实例id会更改;任务棧Id是不变的;
(二)Activity的singleTop启动模式
1)singleTop启动模式
将当前Activity的实例化ID置于棧顶,在该activity做操作时,将不会改变任务棧Id ,也不会更改实例化id,但如果使用“返回”后,重新打开时,二者都会改变;
修改mainfest.xml文件如下:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
**android:launchMode="singleTop">**\\看这里,可以设置Activity的启动模式;
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
(三)Activity的singleTask启动模式
1)singleTask启动模式
当将A activity压棧后,B activity也压棧后,启动A 后,B将会被弹出,并呈现出A的首个activity 的taskID 跟实例化ID , 再次点击返回键后,直接退出activity
修改mainfest.xml文件如下:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
**android:launchMode="singleTask">*看这里!!!singleTask启动模式哟!
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".B_Activity"></activity>
</application>
MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button ;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button_1);
button2 = (Button)findViewById(R.id.button_2);
textView = (TextView)findViewById(R.id.myTextView);
textView.setText(String.format("任务棧ID =%d \n 当前Activity的实例Id = \n %s", getTaskId(), toString()));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
}
});
}
}
B_Activity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class B_Activity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b_);
textView = (TextView)findViewById(R.id.myTextView);
textView.setText(String.format("任务棧ID =%d \n 当前Activity的实例Id = \n %s", getTaskId(), toString()));
}
}
效果图:
点击“启动MainAcitivty” , 后点击“启动B_Activity” , 他们俩均已经入棧,然后点“启动MainActivity”,
棧会将B_Activity弹出,并将MainActivity显示出来,然后点击返回就会退出!!
(四)singleInstance启动模式
singleInstance ,具有独立的taskid 跟 activity id 点击后二者均会改变;
manifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
**android:launchMode="singleInstance">**看这里啦!!!singleInstance启动模式
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".B_Ac
mainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button ;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button_1);
button2 = (Button)findViewById(R.id.button_2);
textView = (TextView)findViewById(R.id.myTextView);
textView.setText(String.format("任务棧ID =%d \n 当前Activity的实例Id = \n %s", getTaskId(), toString()));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, B_Activity.class));
}
});
}
}
B_Activity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class B_Activity extends AppCompatActivity {
private TextView textView;
private Button button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b_);
button3= (Button) findViewById(R.id.button_1);
textView = (TextView)findViewById(R.id.myTextView);
textView.setText(String.format("任务棧ID =%d \n 当前Activity的实例Id = \n %s", getTaskId(), toString()));
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(B_Activity.this , MainActivity.class));
}
});
}
}
运行效果
可以看到任务棧ID ,跟实例化ID
点击B_actityty后
可以看到二者的任务棧ID ,跟实例化ID完全不同,相互独立;这就是singleInstance !!!!
~~~又学了一招!!!