调用的第三个活动将其结果返回到第一个活动,而不是将结果发送到第二个活动?

时间:2012-02-20 12:06:52

标签: android android-activity

情景:

  1. 第一个活动使用startActivityForResult
  2. 启动秒活动
  3. 第二个活动使用startActivityForResult
  4. 启动第三个活动

    预期结果: 第三个(最后一个)活动设置了一个结果,该结果被第二个活动的onActivityResult

    捕获

    目前的结果: 第三(最后)活动设置了第一个活动的onActivityResult

    上的结果

    如何在第二个活动上设置第三个活动的结果第三个(最后一个)活动设置第二个活动的onActivityResult上捕获的结果?

    一些代码段:

    第一项活动

    public class TestProjActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
    
        Log.i("TAAAG", "1st activity - startActivityForResult");
        Intent intent = new Intent(TestProjActivity.this, Activ2.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
        startActivityForResult(intent, 1008);
    }
    
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("TAAAG", "1st activity - onActivityResult");
        if (resultCode == RESULT_OK) {  
            Log.i("TAAAG", String.valueOf(requestCode));
            switch (requestCode) {  
            case 1008:
                String info = data.getExtras().getString("KEY1");
                Log.i("TAAAG", "1st activity - onActivityResult - printing result");
                Log.i("TAAAG", info);
                break;
            }
        }
    }
    

    }

    第二项活动

    public class Activ2 extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Log.i("TAAAG", "2nd activity - startActivityForResult");
        Intent intent = new Intent(Activ2.this, Activ3.class);
    
        startActivityForResult(intent, 1009);
    
        Intent intent2 = new Intent();
        intent2.putExtra("KEY1", "VALUE1");
    
    
        setResult(RESULT_OK, intent2);
        finish();
    }
    
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("TAAAG", "2nd activity - onActivityResult");
        if (resultCode == RESULT_OK) {  
            Log.i("TAAAG", String.valueOf(requestCode));
            switch (requestCode) {  
            case 1009:
                String info = data.getExtras().getString("KEY2");
                Log.i("TAAAG", "2nd activity - onActivityResult - printing result");
                Log.i("TAAAG", info);
                break;
            }
        }
    }
    

    }

    第三项活动

     public class Activ3 extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Log.i("TAAAG", "3rd activity - Inserting Value and finishing");
    
        Intent intent = new Intent();
        intent.putExtra("KEY2", "VALUE2");
    
        setResult(RESULT_OK, intent);
        finish();
    }
    

    }

2 个答案:

答案 0 :(得分:10)

使用活动标记将结果转发到超级活动:

intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);

答案 1 :(得分:3)

我认为你应该在第二个活动的最后一行中删除finish(); ....