Question

I have an Activity that starts another one for result. The fact is that I need to trigger another Activity for result from this one. Is this possible?

As an example Activity A calls Activity B for result, but Activity B needs to call Activity C for result to get the correct data.

I don't know if when I start Activity C for result, Activity B is going to send its (incomplete) result to the first calling activity (Activity A).

In case that it can be done, how should I handle the results from those activities?

Thanks!

Was it helpful?

Solution

This is fine. As long as Activity B is still running it will not trigger anything back to A until it finishes.

Launching A -> B -> C and then closing C will mean B's onActivityResult will trigger, then closing B will trigger A's onActivityResult.

So in Activity B you will need some logic like:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ACTIVITY_C && resultCode == RESULT_XXX) {
        // Get the result from C, etc. Whatever you need to do
        String stuff = data.getStringExtra("XXX");
        Intent output = new Intent();
        output.putExtra(XXX, stuff);
        setResult(RESULT_XXX, output);
        finish();
    }
}

Then you can catch that back in Activity A.

Another option: if you find yourself needing to share data across a lot of activities and the logic seems to be getting complicated, you might like to consider using the Application class to store global state that you can access from all activities.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top