I'm studying launch mode intent flag and activity alias in Android programming.
There are activities (A - B - A - B) in back stack, and I want to start activity A and I also want to reuse A at bottom of back stack. So final state of back stack I want is just (A).
To do so, I write code like this
A (MainActivity)
package com.example.intentflagexampele;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
});
}
}
B (MainActivity2)
package com.example.intentflagexampele;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findViewById(R.id.button).setOnClickListener(v -> {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
startActivity(intent);
});
findViewById(R.id.button2).setOnClickListener(v -> {
Intent intent = new Intent();
ComponentName componentName = new ComponentName(MainActivity2.this,
"com.example.intentflagexampele.SplashActivity");
intent.setComponent(componentName);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentflagexampele">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.IntentFlagExample2">
<activity android:name=".MainActivity2"/>
<activity android:name=".MainActivity">
</activity>
<activity-alias
android:name=".SplashActivity"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
</application>
</manifest>
The problem is that whenever I start SplashActivity with button2, Back Stack become (A - B - A) not (A).
What should I do? Is there something wrong?
This is not a clean application architecture. In general, if you have multiple instances of the same
Activityin the stack, you cannot reliably tell Android to go back to a specific instance of anActivity.You should create separate activities so that you can avoid having multiples instances of the same
Activityin the stack (if you want to be able to return to a specific instance of thatActivity).