是否允许拥有onActivityResult链?

时间:2015-07-07 20:21:55

标签: android android-intent bundle

假设我有一个主要活动A和另外两个活动B和C.

启动意图B,并在某个时刻B启动意图C.

然后C和B一样设置setResult(...)finish(),最后在A中设置onActivityResult(...)

这是允许的吗?它会起作用吗?

3 个答案:

答案 0 :(得分:1)

是的,它会起作用。从C收到结果时,只需完成活动B.但是,当完成B时,活动C的结果不会传播,必要时必须手动设置。

答案 1 :(得分:1)

在活动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
                }

            }
}

在活动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();
                }

            }
}

然后在某点活动C

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

我希望这可以帮到你。

答案 2 :(得分:1)

是的,它会起作用。以下是您可能只需要的过程的概述:

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
}