activitiy重新启动后的setResult()不工作重新启动、工作、activitiy、setResult

2023-09-12 06:02:29 作者:十里桃花

我有三个活动A,B和C。

I have three activities A, B and C.

一个了B与 startActivityForResult(getIntent(),ACTIVITY_B); 和B开始下与 startActivityForResult(getIntent(),ACTIVITY_C); ACTIVITY_B ACTIVITY_C 与整个活动的相同值的常量。

A starts B with startActivityForResult(getIntent(), ACTIVITY_B); and B starts C with startActivityForResult(getIntent(), ACTIVITY_C);. ACTIVITY_B and ACTIVITY_C are constants with same value across activities.

当C返回与RESULT_OK,那么B重新启动与code:

When C returns with RESULT_OK, then B is restarted with the code:

if (resultCode == Activity.RESULT_OK){
    finish();
    startActivityForResult(getIntent(), ACTIVITY_B);
}

这工作得很好。

当B有返回(在点击菜单项),它设置的活动成果。

When B has to return (on clicking a menu item), it sets the activity result.

public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
        case MENU_CONFIRM:
            System.out.println("Setting Result to RESULT_OK");
            setResult(Activity.RESULT_OK);
            finish();
            return true;
    }
    return super.onMenuItemSelected(featureId, item);
}

不过,我可以看到的setResult(Activity.RESULT_OK); 被忽略,因为它总是收到 RESULT_CANCEL 在活动A( onActivityResult )。我使用的是2.3。

However I can see that the setResult(Activity.RESULT_OK); is ignored because it is always received as RESULT_CANCEL in the Activity A (onActivityResult). I'm using 2.3.

任何线索?

推荐答案

没有完成B和开始一个新的,一个不会得到通知,因为它不是它打开对B。 相反,如果C需要返回的东西它结束时,B,创建一个意图,把你的结果值就可以了,而B中签结果如下:

Don't finish B and start a new one, A will not get notified because it's not the B it opened. Instead, if C needs to return something to B when it ends, create an Intent, put your result values on it, and check this result in B:

C:

Intent data = new Intent();
// Store values you want to return on the intent
setResult(RESULT_OK, data);
finish();

B:在on​​ActivityResult

B: in onActivityResult

if (resultCode == Activity.RESULT_OK) {
  // get values from the intent and change B accordingly
}