onActivityResult - resultCode始终为0

时间:2017-02-03 20:18:48

标签: android

我对onActivityResult有问题,无论我做什么,我都无法得到resultCode。

我知道有类似的问题,但最后他们没有帮助我,我无法解决它

MainActivity:方法将打开新的Activity Popup.class

public void openShopView(){
    Intent intent = new Intent(this, Popup.class);
    Bundle b = new Bundle();

    b.putString("which", "ShopMain");
    intent.putExtras(b);
    startActivityForResult(intent, 1);
}

第二个活动:方法,它将使用不同的布局打开另一个活动 Popup.class

shop_c1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getIntent());
            Bundle b = new Bundle();

            b.putString("which", "ShopBuildings");
            intent.putExtras(b);
            startActivity(intent);
            finish();
        }
    });

第三个活动:并且有一个方法应该是setResult并关闭Activity

building2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i("LOG_NEW: ", "" + getCurrentBuildingTable(1) + ", " + checkSlotTable(1));
            if(getCurrentBuildingTable(1) && checkSlotTable(1) == -1) {
                Intent returnIntent = getIntent();
                returnIntent.putExtra("result", 1);
                setResult(RESULT_OK, returnIntent);
                finish();
            }else if (checkSlotTable(1) == -1){
                Log.i("LOG_NEW: ", "Building already exist");
            }
            else{
                Log.i("LOG_NEW: ", "Not enough resources");
            }
        }
    });

最后,MainActivity中有 onActivityResult()

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

    Log.i("LOG_RES: ", "Checking.. " + requestCode + ", " + resultCode);
    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            String result = data.getStringExtra("result");
            Log.i("LOG_RES: ", result);

        }
    }
}

无论我在做什么,我无法启动 if(resultCode == RESULT_OK)循环并且resultCode始终为0 ..

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

必须在第二个活动中调用

var table_rows = document.querySelectorAll("tbody tr"); //or better selector return Array.prototype.map.call(table_rows, function(tr) { return { nom: tr.querySelector(".td-width h3").textContent, description: tr.querySelector(".td-width p").textContent, mail: tr.querySelector('td span a[href^="mailto"]').textContent, site: tr.querySelector('td span a:not([href^="mailto"])').textContent }; }); ,因为第二个活动的意图已在setResult中传递。

但是,您可以将Third Activity的结果代码委托给Second Activity,然后委托给第三个。

第二项活动更改为以下内容:

startActivityForResult

然后在第二个活动

中添加此内容
shop_c1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getIntent());
                Bundle b = new Bundle();

                b.putString("which", "ShopBuildings");
                intent.putExtras(b);
                startActivityForResult(intent,1);
                //Remove finish from here
            }
        });