2

Neither

val intent = Intent(sourceActivity, SomeActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)

nor

val intent = Intent(sourceActivity, SomeActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)

triggers onNewIntent as described by official docs:

FLAG_ACTIVITY_NEW_TASK Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent(). This produces the same behavior as the "singleTask" launchMode value, discussed in the previous section.

They always try to recreate SomeActivity, calling onCreate instead!

Expected behaviours:

  • SomeActivity, running but not on foreground, paused, is brought back to foreground
  • SomeActivity's back stack is preserved
  • SomeActivity receives some info - I want to inform SomeActivity of a success/ failure
  • SomeActivity instance is reused vs. a duplicated is created anew - so FLAG_ACTIVITY_SINGLE_TOP or android:launchMode="singleTop" is not derisable

Interestingly, the Manifest.xml approach works as expected!

<activity
                android:name=".webview.SomeActivity"
                android:launchMode="singleTask"
                android:configChanges="screenSize|orientation" />

What am I missing?

ericn
  • 12,476
  • 16
  • 84
  • 127

1 Answers1

0

This has worked for me :

intent = new Intent(this, HomeScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

If this still doesn't work and you want to clear all activities before starting a new one you can use finishAffinity() before starting a new activity.

Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
Vikas
  • 136
  • 6