2

Suppose I have a main activity A and two other activities B and C.

A launches intent B and at some point B launches intent C.

Then C sets setResult(...) and finish()'s, as does B, finally ending at onActivityResult(...) in A.

Is this allowed; will it work?

  • Yes. I'm running into difficulties, and rather than debug something which can't be debugged I wanted to ask those existing experienced users. –  Jul 07 '15 at 20:25
  • 1
    `onActivityResult` fires only for direct started activities. So you have to override `onActivityResult` both for A and B. – anil Jul 07 '15 at 20:25
  • I'm already overriding for A and B. On the assumption it is possible, I'll keep debugging. –  Jul 07 '15 at 20:27
  • In the startActivityForResult() method call you can specify a result code to determine which activity you started. This result code is returned to you. http://www.vogella.com/tutorials/AndroidIntent/article.html – Kristy Welsh Jul 07 '15 at 20:28

3 Answers3

1

Yes, it will work. Just finish activity B when recieve the result from C. However the result from activity C will not be propagated when finishing B, you will have to set it manually if necessary.

Mojo Risin
  • 8,136
  • 5
  • 45
  • 58
1

In activity A:

Intent intent = new Intent(this, B.class);
startActivityForResult(intent, 1);

....

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == 1){
            if (resultCode == RESULT_OK){

                Bundle bundle = data.getExtras();
                String code = "";

                try{
                    code = bundle.getString("code");
                } catch (Exception e){
                    e.printStackTrace();
                }

                if (!code.equals("")){
                    //do something
                }

            }
}

In Activity B:

Intent intent = new Intent(this, C.class);
startActivityForResult(intent, 1);

...
 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == 1){
            if (resultCode == RESULT_OK){

                Bundle bundle = data.getExtras();
                String code = "";

                try{
                    code = bundle.getString("code");
                } catch (Exception e){
                    e.printStackTrace();
                }

                if (!code.equals("")){
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString("code", code);
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                    finish();
                }

            }
}

Then in Activity C at some point:

Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("code", code);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();

I hope this can help you.

lfal
  • 228
  • 2
  • 13
1

Yes it will work. Here's an overview of the process you probably just need:

Activity A {
    startActivityForResult() // start activity B

    onActivityResult() // receive B's result
}

Activity B {
    getIntentFromA()
    startActivityForResult(); // start activity C

    onActivityResult() {    
        // receive C's result
        setResult(c's result); <-- this will pass back to A
    }
}

Activity C {
    getIntentFromB()

    setResult(c's result); <-- this will pass back to B
}
Peanuts
  • 352
  • 3
  • 6