Skip to content Skip to sidebar Skip to footer

Finish() Activity Twice In Android?

Okay say your using a app, and you opened a new activity and then opened another, you can end the activity your on by using finish(); and your back one activity, but how can you go

Solution 1:

Use the flag Intent.FLAG_ACTIVITY_CLEAR_TOP.

  Intent intent = newIntent(this,A.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  startActivity(intent);

From the documentation:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

So effectively, if you have A -> B -> C, and you intent to A with that flag set, B and C will close.

Solution 2:

I believe that will start a new activity, not back up to the original one. It sounds like you want to finish the last and middle activities. If you start the last activity with startActivityForResult, you can then override onActivityResult in the middle activity, and call finish() from there.

Post a Comment for "Finish() Activity Twice In Android?"