Fabián Romo
Fabián Romo

Reputation: 329

How can I not return to a previous activity after using startActivity?

Assuming I have three activities with Android Studio:

Act_A

Act_B

Act_C

The Act_A calls Act_B, and Act_B calls Act_C:

Intent intent = new Intent(Act_A.class,Act_B.class);
startActivity(intent);

...

Intent intent = new Intent(Act_B.class,Act_C.class);
startActivity(intent);

In Act_C a process is executed in which it should automatically be returned to Act_B, for which something similar is done:

Intent intent = new Intent(Act_C.class,Act_B.class);
startActivity(intent);

At this point, if the user presses the return button, it should be returned to Act_A, but it happens to be returned to Act_C, if you press once again the return button returns to Act_B.

My questions are:

Is there a way to "delete" the previous state so that it doesn't return to the previous activity or is there a way to modify it to return to the activity that I want?

The problem is that I have to return a value from Act_C to Act_B and I can not use finish(), something similar to the following:

In Act_C:

Intent intent = Act_B.newIntent(getActivity(),5);// return 5

startActivity(intent);

Thanks

Upvotes: 0

Views: 1242

Answers (4)

FabioLaranjeira
FabioLaranjeira

Reputation: 29

In Act_C a process is executed in which it should automatically be returned to Act_B

If you just want to return a value from a specific activity don't use startActivity(Intent), use startActivityForResult(Intent, int) and deal with the return values on the callback (OnActivityResult). Then overwrite the onFinish() method so that you can setResult() upon exit. I can give you more context on this if you need.

Upvotes: 0

LarsH
LarsH

Reputation: 27995

You can customize the back stack of your activity if you really need to, but in this case it seems that what you want is just to go back to B from C, instead of starting B from C.

In other words, in order to accomplish "In Act_C a process is executed in which it should automatically be returned to Act_B", do the following instead of startActivity():

    super.onBackPressed();

This will return the app to Act_B. Then if the user presses the back button, they will return to Act_A as you specified.

Upvotes: 0

Tyson Miller
Tyson Miller

Reputation: 201

Basically you can either call finish() on your activity, use the noHistory flag for the activity in your AndroidManifest.xml, or add the intent flag Intent.FLAG_ACTIVITY_NO_HISTORY to your intent.

This post should help you out: Removing an activity from the history stack

Upvotes: 0

S Haque
S Haque

Reputation: 7271

You can call the finish() method of an Activity. It will get you back to Activity_B while you are in Activity_C

Upvotes: 1

Related Questions