How can I clear activity stack and start only one activity?

139 Views Asked by At

I launched next activity with these flags to clear the backstack.

addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)

and also tried with

addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)

I've added this too.

addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

But all of the result are the same like this.

D/activity-stack: idx: 0
     num: 1, base-name: com.example.ui.result.ResultActivity, top-name: com.example.ui.result.ResultActivity
D/activity-stack: idx: 1
     num: 1, base-name: com.google.android.apps.nexuslauncher.NexusLauncherActivity, top-name: com.google.android.apps.nexuslauncher.NexusLauncherActivity
D/activity-stack: idx: 0
     num: 1, base-name: com.example.ui.result.OrderActivity, top-name: com.example.ui.result.OrderActivity
D/activity-stack: idx: 1
     num: 1, base-name: com.example.ui.result.ResultActivity, top-name: com.example.ui.result.ResultActivity
D/activity-stack: idx: 2
     num: 1, base-name: com.google.android.apps.nexuslauncher.NexusLauncherActivity, top-name: com.google.android.apps.nexuslauncher.NexusLauncherActivity
D/activity-stack: idx: 0
     num: 1, base-name: com.example.ui.result.PaymentActivity, top-name: com.example.ui.result.PaymentActivity
D/activity-stack: idx: 1
     num: 1, base-name: com.example.ui.result.OrderActivity, top-name: com.example.ui.result.OrderActivity
D/activity-stack: idx: 2
     num: 1, base-name: com.example.ui.result.ResultActivity, top-name: com.example.ui.result.ResultActivity
D/activity-stack: idx: 3
     num: 1, base-name: com.google.android.apps.nexuslauncher.NexusLauncherActivity, top-name: com.google.android.apps.nexuslauncher.NexusLauncherActivity
D/activity-stack: idx: 0
     num: 1, base-name: com.example.ui.result.CompleteActivity, top-name: com.example.ui.result.CompleteActivity
D/activity-stack: idx: 1
     num: 1, base-name: com.example.ui.result.OrderActivity, top-name: com.example.ui.result.OrderActivity
D/activity-stack: idx: 2
     num: 1, base-name: com.google.android.apps.nexuslauncher.NexusLauncherActivity, top-name: com.google.android.apps.nexuslauncher.NexusLauncherActivity

As you can see, it cleared only the oldest one. How can I clear the backstack clearly?

What I expect is:

D/activity-stack: idx: 0
     num: 1, base-name: com.example.ui.result.Completectivity, top-name: com.example.ui.result.CompleteActivity
D/activity-stack: idx: 1
     num: 1, base-name: com.google.android.apps.nexuslauncher.NexusLauncherActivity, top-name: com.google.android.apps.nexuslauncher.NexusLauncherActivity
1

There are 1 best solutions below

1
On

Actually, My code had nothing wrong. But I set singleInstance to that activity, so it didn't work.

because singleInstance seems higher priority, so it won't be overwritten by singleTask(NEW_TASK+CLEAR_TASK or CLEAR_TOP)