AndroidManifest.xml给指定的
(1)standard(标准模式)
新建FirstActivity,FirstActivity.java中onCreate方法中按钮事件
Button btn_to_this = findViewById(R.id.btn_to_this); btn_to_this.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(FirstActivity.this, FirstActivity.class); startActivity(intent); } });
在activity_first.xml中文本和按钮
android:id="@+id/tv_"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="This is FirstActivity"
android:textSize="24sp" />
(2)singleTop
AndroidManifest.xml中指定FirstActivity的android:launchMode属性值singleTop
android:name=".FirstActivity" android:launchMode="singleTop">
新建SecondActivity,SecondActivity.java中onCreate方法中按钮事件
Button btn_second = findViewById(R.id.btn_second); btn_second.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(SecondActivity.this, FirstActivity.class); startActivity(intent); } });
activity_second.xml中文本和按钮
android:id="@+id/tv_"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="This is SecondActivity"
android:textSize="24sp" />
FirstActivity.java中增加btn_to_second按钮事件
Button btn_to_second = findViewById(R.id.btn_to_second); btn_to_second.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(FirstActivity.this, SecondActivity.class); startActivity(intent); } });
activity_first.xml中增加btn_to_second按钮
(3)singleTask
android:name=".FirstActivity" android:launchMode="singleTask">
(4)singleInstance