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
singleTasklaunchMode for the activity which you starts on tap of restart.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
singleTaskActivity 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, anIntentwould be sent to thesingleTask Activitythrough the lovelyonNewIntent()method.So you need to write activity entry in manifest as
Please see
android:taskAffinitydocumentation 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.