2

In my application I have a sequence of 5 activitys, which from the second, there is the restart button, this should go back to first, the problem is that the stack still holds, which creates a problem, since the user can click back. Clearing the entire stack is not an option as there is an activity before these 5, and if I clean it, it will also be erased.

Using fragments is not an option

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
Woton Sampaio
  • 469
  • 6
  • 24
  • try `android:noHistory="true"` in your manifest or `intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);` – Hemant Parmar Jul 07 '18 at 06:58
  • Can you explain it bit more? – Pankaj Kumar Jul 07 '18 at 07:01
  • @PankajKumar, So, the first activity is a menu, when clicking on an option it opens another activity that is a sequence, type, in this new I click it goes to another, then click the other and it goes to next, but if I click " restart "it goes back to the first after the menu, so I would have to erase the others and leave the menu alone, understand? – Woton Sampaio Jul 07 '18 at 15:59

1 Answers1

1

You need to use singleTask launchMode for the activity which you starts on tap of restart.

singleTask

If there is no that singleTask Activity instance existed in the system yet, new one would be created and simply placed on top of stack in the same Task.

But if there is an existed one, all of Activities placed above that singleTask Activity would be automatically and cruelly destroyed in the proper way (lifecycle trigged) to make that an Activity you want to appear on top of stack. In the mean time, an Intent would be sent to the singleTask Activity through the lovely onNewIntent() method.


So you need to write activity entry in manifest as

<activity
    android:name=".YouSecondActivity"
    android:label="singleTask launchMode"
    android:launchMode="singleTask">

Please see android:taskAffinity documentation also. Although you do not require here but you should know about it.

References are Understand Android Activity's launchMode: standard, singleTop, singleTask and singleInstance and Android Activity “launchMode” Explained , Must know for Android Development.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186