Problems with the onActivityResult

1

I want to send a Json from one activity to another. In the first activity, inside the onClick of a button I have the following:

Intent i = new Intent(PrimeraActivity.this, SegundaActivity.class);
startActivityForResult(i, REQUEST_CODE);

In the second activity, I use a method to return the data if they insert the correct credentials:

public void iniciarActivity() {
    String pass = mPasswordView.getText().toString();
    String pass2 = Objeto.getPassword();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if (Objects.equals(pass, pass2)) {
            Toast toast = Toast.makeText(this, "Credenciales Correctos", Toast.LENGTH_LONG);
            toast.show();
            Intent intent = new Intent();
            gson = new Gson();
            String myjson = gson.toJson(objeto);
            Log.i("baliza", "json:" +myjson);
            intent.putExtra("myjson", myjson);
            setResult(RESULT_OK, intent);
            finish();
        } else {
            Toast toast = Toast.makeText(this, "Credenciales inCorrectos", Toast.LENGTH_LONG);
            toast.show();
        }
    }
}

In the first activity, the onActivityResult method:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.i("baliza", "onActivityResult main");
    Log.i("baliza", "requestCode main: "+requestCode);
    // check if the request code is same as what is passed  here it is 2
    if (requestCode == REQUEST_CODE_LOGIN) {
        if (resultCode == RESULT_OK) {
            if (data.getStringExtra("myjson") != null) {
                Log.i("baliza", "onActivityResult main if");
                gson = new Gson();
                objeto = gson.fromJson(data.getStringExtra("myjson"), ClaseObjeto.class);
            }
        }
    }
}

The problem is that it does not enter the onActivityResult. Would I like to know why it does not happen there? And if it is the most correct way to do it? Thanks !!!

    
asked by tripossi 28.03.2017 в 00:45
source

1 answer

0

It is important to ensure that it is executed:

 setResult(RESULT_OK, intent);

otherwise it will not be called onActivityResult() when returning to your Activity .

    
answered by 28.03.2017 в 03:54