ƒernando Valle
ƒernando Valle

Reputation: 3714

Close an specific activity android

Just, How can I close an specific activity of my application?

for example I have the activities:

A1 > B1 > C1

And in C1 I can go to A activity again with new params so when I will open a new B (B2), I want to close older activities: A1, B1, C1.

Being the new result:

A2 > B2

Where I can go back to A2 after of B2, and finish the app from A2.

Upvotes: 0

Views: 1992

Answers (2)

Ilya Gazman
Ilya Gazman

Reputation: 32226

You have two ways to do this:

You can use activity declaration such as single top. Or you can pass data between activities using start for result. Then on onActivityResult just call finish();

Upvotes: 3

codeMagic
codeMagic

Reputation: 44571

This should work for you:

Intent i = new Intent(C.this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i); 

FLAG_ACTIVITY_CLEAR_TOP will clear all other Activities off of the stack. FLAG_ACTIVITY_CLEAR_TASK

this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started.

Intent Flags

Note as stated by @Dalmas FLAG_ACTIVITY_CLEAR_TASK only works for API >= 11

Upvotes: 0

Related Questions