I had a similar case when I needed to finish all the actions. Here is one way to do this without finishAndRemoveTask ().
Make all your activities an extension of the base class with the following things:
private Boolean mHasParent = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); Bundle extras = intent.getExtras(); if (extras != null) { mHasParent = extras.getBoolean("hasParent", false); } } // Always start next activity by calling this. protected void startNextActivity(Intent intent) { intent.putExtra("hasParent", true); startActivityForResult(intent, 199); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (requestCode == 199 && resultCode == FINISH_ALL) { finishAllActivities(); } } protected void finishAllActivities() { if (mHasParent) { // Return to parent activity. setResult(FINISH_ALL); } else { // This is the only activity remaining on the stack. // If you need to actually return some result, do it here. Intent resultValue = new Intent(); resultValue.putExtra(...); setResult(RESULT_OK, resultValue); } finish(); }
Just call finishAllActivities() on any action, and all actions will unfold. Of course, if you don't care what result the last action returns, the code can be made much simpler.
source share