Home > Software design >  startActivityForResult usage for 3 activity Android
startActivityForResult usage for 3 activity Android

Time:08-19

Hey guys I have 3 activity,

Activity A -> Activity B -> Activity C

This flow I want to perform in my application. I want this scenario in my Activity C

Scenario 1

On Back press from Activity C to open Activity B then again back press then open Activity A

Scenario 2

When something submit on Activity C, I want to open Activity A and I want to hide Activity B

So how can I do this through startActivityForResult. I check through this stackoverlow, but nothing works. Thanks

CodePudding user response:

I'm assuming that the 3 activities are opened in order A -> B -> C, so they are in the backstack in that order. You can check the return values from Activity C, and act accordingly.

In Activity C, setResult(Activity.RESULT_CANCELLED) during onCreate(). That will make it the default return value when activity C is closed (back press or otherwise)`.

Then setResult(Activity.RESULT_OK) whenever something is submitted. After that, you can fininsh() that activity.

Whenever Activity C get closed, in Activity B onActivityResult(), check the return value. If it's RESULT_OK, call finish() (which will return to Activity A). Otherwise, do nothing, so Activity B will stay open

  • Related