Mahsa
Mahsa

Reputation: 271

Android-finish function in activity didn't work

I extend activity A, B, C from MainActivity. A start in first and A start B and B start C. when I use finish() in onclick special Button in activity C, Activity A is start, but I want activity B start. What I do for this case?

Upvotes: 2

Views: 428

Answers (3)

Ankit
Ankit

Reputation: 1068

Start all activity like below:

Intent i = new Intent(this,NewActivity.class);

startActivity(i);

don't add finish() method, then inside button click just call finish() method. It will bring you back to previous activity.

Upvotes: 6

Narendra Sorathiya
Narendra Sorathiya

Reputation: 3830

AActivity.java

Intent i = new Intent(this,BActivity.class);
startActivity(i);

BActivity.java

Intent i = new Intent(this,CActivity.class);
startActivity(i);

CActivity.java

Intent i = new Intent(this,BActivity.class);
startActivity(i);
finish();

Upvotes: 0

Egor
Egor

Reputation: 40228

You can't reliably expect Activity B to be on the stack when Activity C finishes since Android can destroy a background Activity if it needs memory. If your use case suggests that Activity B should always become visible when Activity C is closed, you should start it explicitly, as described in Navigating Up to Parent Activity. In your case, I think it's appropriate to use NavUtils.navigateUpFromSameTask(this) in onBackPressed().

Upvotes: 1

Related Questions