标准启动模式:
MainActivity.java:
package com.imooc.launchmode; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); tv.setText(String.format("TaskID:%d ,Curren Activity id%s",getTaskId(),toString())); findViewById(R.id.btnStarSelf).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ } }); } }
activity_main.xml:
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnStarSelf" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动MainActivity" tools:layout_editor_absoluteX="100dp" tools:layout_editor_absoluteY="157dp" /> </androidx.constraintlayout.widget.ConstraintLayout>
SingleTop:
activity_main.xml:
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnMainAty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动MainActivity" tools:ignore="MissingConstraints" /> <Button android:id="@+id/btnStarBAty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动BAty" tools:ignore="MissingConstraints" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.imooc.launchmode; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); tv.setText(String.format("TaskID:%d ,Curren Activity id%s",getTaskId(),toString())); findViewById(R.id.btnMainAty).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ startActivity(new Intent(MainActivity.this,MainActivity.class)); } }); findViewById(R.id.btnStarBAty).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,BAty.class)); } }); } }
BAty.java:
package com.imooc.launchmode; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class BAty extends AppCompatActivity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_baty); tv = (TextView) findViewById(R.id.tv); tv.setText(String.format("TaskID:%d ,Curren Activity id%s",getTaskId(),toString())); findViewById(R.id.btnMainAty).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ startActivity(new Intent(BAty.this,MainActivity.class)); } }); findViewById(R.id.btnStarBAty).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(BAty.this,BAty.class)); } }); } }
activity-baty.xml:
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".BAty"> <TextView android: android:text="Hello world!" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btnMainAty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动MainActivity" tools:ignore="MissingConstraints" /> <Button android:id="@+id/btnStarBAty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动BAty" /> </androidx.constraintlayout.widget.ConstraintLayout>