在AndroidManifest.xml文件中,各个Activity的配置如下:
<activity
android:name=".ActivityMain"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".ActivitySub1">
<intent-filter>
<action android:name="android.intent.action.ActivitySub1"/>
<category android:name="android.intent.category.ActivitySub1"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".ActivitySub2">
<intent-filter>
<action android:name="android.intent.action.ActivitySub2"/>
<category android:name="android.intent.category.ActivitySub2"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".ActivitySub3">
<intent-filter>
<action android:name="android.intent.action.ActivitySub3"/>
<category android:name="android.intent.category.ActivitySub3"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
ActivityMain的布局文件activity_main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.horrorkwangmail.a20171208_appnameandactivitytitlename.ActivityMain">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="This is Activity main"
android:textAllCaps="false"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/buttonActivitySub1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:text="Go to Activity sub 1"
android:textAllCaps="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
android:onClick="buttonClick"/>
<Button
android:id="@+id/buttonActivitySub2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:text="Go to Activity sub 2"
android:textAllCaps="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonActivitySub1"
android:onClick="buttonClick"/>
<Button
android:id="@+id/buttonActivitySub3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:text="Go to Activity sub 3"
android:textAllCaps="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonActivitySub2"
android:onClick="buttonClick"/>
</android.support.constraint.ConstraintLayout>
ActivityMain加载的menu文件menu_activity_main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/itemActivitySub1"
android:title="Activity sub 1"/>
<item
android:id="@+id/itemActivitySub2"
android:title="Activity sub 2"/>
<item
android:id="@+id/itemActivitySub3"
android:title="Activity sub 3"/>
</menu>
ActivityMain.java的Button响应代码如下:
public void buttonClick(View v) //点击各个Button的响应
{
switch (v.getId())
{
case R.id.buttonActivitySub1:
Intent intent1=new Intent("android.intent.action.ActivitySub1");
intent1.addCategory("android.intent.category.ActivitySub1");
startActivity(intent1);
break;
case R.id.buttonActivitySub2:
Intent intent2=new Intent("android.intent.action.ActivitySub2");
intent2.addCategory("android.intent.category.ActivitySub2");
startActivity(intent2);
break;
case R.id.buttonActivitySub3:
Intent intent3=new Intent("android.intent.action.ActivitySub3");
intent3.addCategory("android.intent.category.ActivitySub3");
startActivity(intent3);
break;
default:
break;
}
}
ActivityMain.java的item响应代码如下:
public boolean onOptionsItemSelected(MenuItem item) //点击menu的各个item的响应
{
switch (item.getItemId())
{
case R.id.itemActivitySub1:
Intent intent1=new Intent();
intent1.setAction("android.intent.action.ActivitySub1");
intent1.addCategory("android.intent.category.ActivitySub1");
startActivity(intent1);
break;
case R.id.itemActivitySub2:
Intent intent2=new Intent();
intent2.setAction("android.intent.action.ActivitySub2");
intent2.addCategory("android.intent.category.ActivitySub2");
startActivity(intent2);
break;
case R.id.itemActivitySub3:
Intent intent3=new Intent();
intent3.setAction("android.intent.action.ActivitySub3");
intent3.addCategory("android.intent.category.ActivitySub3");
startActivity(intent3);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
而在ActivitySub1.java文件、ActivitySub2.java文件和ActivitySub3.java文件中,使用隐式Intent跳转到Activity main中,使用的代码如下:
Button buttonBackToActivityMain=findViewById(R.id.buttonBackToActivityMain);
buttonBackToActivityMain.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent=new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
startActivity(intent);
}
});
真机测试视频如下: