不要从其他应用程序接收意图

时间:2019-01-08 00:20:55

标签: android onactivityresult

在第一个应用程序中,我使用以下代码运行第二个应用程序:

 Intent myIntent = new Intent();
            myIntent.setClassName("com.XXX.XXX", "com.XXX.XXX.Main");
            myIntent.putExtra("date", date);
            startActivityForResult(myIntent, 1);

在应用B中,我将一些额外的东西发送回了应用B:

Intent resultIntent = getIntent();
        resultIntent.putExtra("extra", value);
        setResult(Activity.RESULT_OK, resultIntent);
        finish();

在App A中,我想获得额外的价值:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        if (requestCode == 1) {
            if (resultCode == RESULT_OK){
                String street = data.getStringExtra("extra");
            }
        }
}

当我运行它时,resultCode始终为0(RESULT_CANCELED),并且onActivityResult中的数据为空。我的代码有什么问题?

2 个答案:

答案 0 :(得分:0)

尝试更改此设置

Intent resultIntent = getIntent();

setResult(Activity.RESULT_OK, resultIntent); 

对此

Intent resultIntent = new Intent();

setResult(Activity.RESULT_OK, resultIntent); 

答案 1 :(得分:0)

尝试从下面的代码中删除super.onActivityResult(requestCode, resultCode, data);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
        //super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK){
                String street = data.getStringExtra("extra");
            }
        }
}