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
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.