There is no a straight forward way to do this, I did it this way hope this helps you.
Try this it works fine with me
// clear whole activity stack
Intent intent = new Intent("clearStackActivity");
intent.setType("text/plain");
sendBroadcast(intent);
// start your new activity
Intent intent = new Intent(OrderComplete.this,
MainActivity.class);
startActivity(intent);
Step : 1
Put these line in onCreate() method of all Activities or if you have any base activity you can put it there , then no need to put in all activities.
private KillReceiver clearActivityStack;
clearActivityStack = new KillReceiver();
registerReceiver(clearActivityStack, IntentFilter.create("clearStackActivity", "text/plain"));
Step : 2
Put this class in your Base activity
private final class KillReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
}
Explanation :
In above code we created our custom broadcast receiver. And we are registering it in base activity i.e in all activity which invokes.
When we wants to finish all activities we just broadcast intent, so all activity which are register this receiver will notify and finish them self.