0

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

Fabián Romo
  • 319
  • 2
  • 14

4 Answers4

1

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

S Haque
  • 6,881
  • 5
  • 29
  • 35
  • 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: Intent intent = Act_B.newIntent(getActivity(),5);// return 5 startActivity(intent); – Fabián Romo Apr 18 '17 at 16:09
  • the you need to use `startActivityForResult()` to open Act_C from Act_B – S Haque Apr 18 '17 at 16:14
0

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

Community
  • 1
  • 1
Tyson Miller
  • 201
  • 1
  • 2
0

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.

LarsH
  • 27,481
  • 8
  • 94
  • 152
  • Assuming I have enabled the use of menus: @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) In the menu there is a button with a return arrow, what is the event associated with it? – Fabián Romo Apr 18 '17 at 15:33
  • @FabiánRomo: In general, it's the `onOptionsItemSelected()` method that you need to override (https://developer.android.com/guide/topics/ui/menus.html#RespondingOptionsMenu). Beyond that, it's hard to tell much without seeing your code. You may have to create a new question and show your code. – LarsH Apr 18 '17 at 15:47
0

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.