0

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
c-an
  • 3,543
  • 5
  • 35
  • 82
  • Have you tried finish() method after every time a new activity is called? This will not track or allow user to go back to previous activity if new one is called – Zaid Zakir Jul 06 '21 at 04:36
  • I need to consider back pressed which is the app needs to go back to the previous screen, so, I shouldn't call finish() every time. – c-an Jul 06 '21 at 04:38
  • try one of these if you havn't or FLAG_ACTIVITY_NO_HISTORY – Zaid Zakir Jul 06 '21 at 04:40
  • Well, that activity will not be on the stack. It needs to be in the stack.. I just need to clear up the stack without detouring the solution. – c-an Jul 06 '21 at 04:43
  • @ZaidZakir, I am sorry I set singleInstance in manifest.. And the code works fine. – c-an Jul 06 '21 at 04:59

1 Answers1

0

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)

c-an
  • 3,543
  • 5
  • 35
  • 82
  • Using the special launch modes `singleInstance` or `singleTask` usually creates more problems than it solves. You only need this in very special cases (like when you create a HOME-screen replacement). – David Wasser Jul 06 '21 at 15:44