Question

On Activity A sending intent with some string values:

public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case R.id.Changepin:{
            Intent i = new Intent(MainActivity.this,ChangePwd.class);
            i.putExtra("plain1", plaintext);
            i.putExtra("cipher1", ciphertext);
            startActivityForResult(i,100);
            //startActivity(i);
            //finish();
        }
        break;
        default:{
            Log.e("","onOptionsItemSelected() Unknown menu event.");
        }
        break;
        }
        return true;
    }

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

    super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==100){
            if(resultCode==10){

            String ciphertext1 = data.getStringExtra("changedcipher");
            Toast.makeText(MainActivity.this, ciphertext1, Toast.LENGTH_LONG).show();
            Log.e("mainactivity", "" + ciphertext1);
    }
}
}

On Activity B calling setResult() with same result code as it matches with the result code onActivityResult() method on Activity A:

public void setNewPin(){
String ciphertext1=null;    Intent i = new Intent(ChangePwd.this,MainActivity.class);
try{
ciphertext1 = crypthelper.encrypt(plaintext);
}catch (Exception e) {
}
i.putExtra("changedcipher", ciphertext1);
setResult(10,i);
Toast.makeText(ChangePwd.this, ciphertext1, Toast.LENGTH_LONG).show();
finish();
}
Was it helpful?

Solution

The request codes you are using to denote call to Activity B are mis-matching here. The one you have put to start the Activity B is 2

startActivityForResult(i,2);

and then in onActivityResult you check,

if(requestCode==100){ .. }

hence the issue. Change it to,

if(requestCode==2){ .. }

and it shall work. :)

Edit -

Change Intent i = new Intent(ChangePwd.this,MainActivity.class); to

Intent i = new Intent();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top