2

My problem is this ... I have 3 Activities:

(ActivityA), (ActivityB) and (ActivityC)

From ActivityA to ActivityB I use the following code:

val intent = Intent(this, ActivityB::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
startActivity(intent)
finish()

From ActivityB to ActivityC I also use this code:

val intent = Intent(this, ActivityC::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
startActivity(intent)
finish()

But if I give onBackPressed or go to any other App and return to my App, instead of returning to ActivityC, it goes to ActivityA.

RKRK
  • 1,284
  • 5
  • 14
  • 18
  • Btw, you can combine multiple flags together with the `or` bitwise operator (aka `intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or /* … */` instead of calling `intent.addFlags` multiple times). See https://stackoverflow.com/questions/45995425/how-to-combine-intent-flags-in-kotlin for more info. – Edric Jun 30 '19 at 05:48
  • chech this library. Annotation based: https://github.com/kostasdrakonakis/android_navigator – Kostas Drak Jun 30 '19 at 13:38

4 Answers4

1

Use this instead

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(intent)
finish()
Mohimenul Joaa
  • 646
  • 6
  • 10
0

You'll need to remove Intent.FLAG_ACTIVITY_NO_HISTORY from the Intent for starting Activity C, as the flag prevents the new activity from being added to the back stack and finishes the activity when it is navigated away from. This means your app will start with it's launcher activity, which I assume is Activity A. You can read more about this flag here in the documentation.

Elli White
  • 1,440
  • 1
  • 12
  • 21
0

From Activity A to Activity B do like this -

val intent = Intent(this, ActivityB::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()

And from Activity B to Activity C do like this -

val intent = Intent(this, ActivityC::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()
Anupam
  • 2,845
  • 2
  • 16
  • 30
0

You have to remove intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY).

It does clear the history of current activity.

Roman
  • 2,464
  • 2
  • 17
  • 21