Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult - resultCode is always 0

Tags:

android

I have problem with onActivityResult, whatever I'm doing I can't get resultCode right.

I know that there are similar questions but at the end they didn't help me and I couldn't fix it

MainActivity: method which will open new 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);
}

Second Activity: method which will open yet another Activity Popup.class just with different layout

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();
        }
    });

Third Activity: and there is method which should setResult and close 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");
            }
        }
    });

At the end there is onActivityResult() from MainActivity

@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);

        }
    }
}

Whatever I'm doing I can't start if(resultCode == RESULT_OK) loop and resultCode is always 0..

Thanks for help

like image 525
fairon201 Avatar asked Jul 20 '26 21:07

fairon201


1 Answers

setResult must be called in Second Activity, since intent of second activity was passed in startActivityForResult.

However, you can delegate the result code of Third Activity to Second Activity, then to third.

Change your Second Activity to something like this:

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
            }
        });

then also add this in Second Activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==1){
    setResult(resultCode,data);
    }

    finish();
}
like image 169
Saurabh Avatar answered Jul 22 '26 10:07

Saurabh