Search code examples
androidandroid-intentactivity-finish

Remove all the previous activites except home activity


How to remove all the activities except HomeActivity.

ex: A(HomeActivity)->B->C->D now call B activity again removing C & D activities. I'm sending some data from D activity to B activity through intent. I have next button if i click next button then this process will be done. If I backpress then my process will be D->C->B->A


Solution

  • When you're going from D->B, use intent.setFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP); It will close all of the other activities on top of it and this Intent will be delivered to the (now on top) old activity as a new Intent.

    FLAG_ACTIVITY_CLEAR_TOP