返回时没有调用onActivityResult

时间:2016-01-21 21:06:34

标签: android onactivityresult

我正在尝试将数据向后传递给一个活动,但我永远无法调用我的onActivityResult函数。当我开始新活动时,我创建了一个像常规

这样的新意图
 Intent intent = new Intent(this, NewLoanCost.class);

 intent.putExtra("defaultsArray", jDefaultsArray.toString());
 intent.putExtra("loanSelection", loanSelection);
 intent.putExtra("buyerSellerSelection", buyerSellerSelection);

 startActivity(intent);

当我想回到之前的活动时,我会覆盖后退按钮以创建新意图并存储数据

@Override
public void onBackPressed() {
    Intent intent = new Intent();

    intent.putExtra("fullCosts", fullCosts.toString());

    setResult(RESULT_OK, intent);

    super.onBackPressed();
}

但是在第一个活动中,我甚至无法调试吐司出现。我错过了一些公然的东西吗?

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);
    Toast.makeText(this, "onActivityResult", Toast.LENGTH_SHORT).show();
    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            try {
                jDefaultsArray = new JSONArray(data.getStringExtra("fullCosts"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

3 个答案:

答案 0 :(得分:5)

要收到结果,您应使用startActivityForResult代替startActivity

答案 1 :(得分:1)

使用startActivityForResult

用这个替换你的代码,然后它应该工作:

 Intent intent = new Intent(this, NewLoanCost.class);

 intent.putExtra("defaultsArray", jDefaultsArray.toString());
 intent.putExtra("loanSelection", loanSelection);
 intent.putExtra("buyerSellerSelection", buyerSellerSelection);

 startActivityForResult(intent);

答案 2 :(得分:1)

将您的代码更改为

 Intent intent = new Intent(this, NewLoanCost.class);

 intent.putExtra("defaultsArray", jDefaultsArray.toString());
 intent.putExtra("loanSelection", loanSelection);
 intent.putExtra("buyerSellerSelection", buyerSellerSelection);

 startActivityForResult(intent, 1);

第二个参数是一个int,其中包含您将在onActivityResult

中使用的请求代码