onActivityResult does not return setResult

Asked

Viewed 339 times

1

What happens is that I have an Activity Client where I have 2 buttons, one for registration and another for consultation, when I call the registration opens an Activity with a Tabhost with 2 fragments inside, in the query opens an Activity with listview and such, but in it has 2 buttons, one for me to edit and another to register a new Client (don’t ask me why, my boss asked so), my difficulty is the following, I am on the registration screen, I will fetch the neighborhood and call the startActivityForResult, if I called the registration screen direct from the sign up button the settResult of the Neighborhood Query returns to Intent, if I call the Customer Consultation Activity and I look for a Neighborhood, does not return setResult values.

OBS: use the same Activity to Register and Edit.

calling on startActivityForResult:

private void abrirConsBairro(View view) {
    Button btnPesqBairro = (Button) view.findViewById(R.id.btnAbrirBairro);

    btnPesqBairro.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent bairroCons = new Intent(getActivity(), BairroConsActivity.class);
            startActivityForResult(bairroCons, 0);
        }
    });
}

Inside the Neighborhood Consultation:

public void selecionarBairro(View view) {
    if (listaBairroCons.getCheckedItemPosition() == -1) {
        Mensagens.mensagemCustomizada("Aviso!", "Selecione um Bairro primeiro!", BairroConsActivity.this);
        return;
    }
    Bundle bundle = new Bundle();
    bundle.putParcelable("objBairro", objBairro);
    Intent i = new Intent();
    i.putExtras(bundle);
    setResult(1, i);
    finish();
}

onActivityResult the registration screen:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null) {
        objCliente.getEndereco().setBairro((Bairro) data.getParcelableExtra("objBairro"));
        edtBairro.setText(objCliente.getEndereco().getBairro().getNome());
        edtMunicipio.setText(objCliente.getEndereco().getBairro().getMunicipio().getNome());
        edtEstado.setText(objCliente.getEndereco().getBairro().getMunicipio().getEstado().getNome());
    }
}

OBS²: ultilizo value 0 and 1 in an attribute to know when it is registration and when it is edition;

  • lavaprato, remembering that the second parameter of the startActivityForResult is the requestCode and the first parameter of setResult is the resultCode. What you can do is pass a parameter within the Bundle whether it’s editing or creating instead of using 1 and 0.

  • Hello Wakim, yes I understand that, but what happens is that when calling the onActivityResult of the query Activity it does not return the setResult, debugged and saw that the setResult arrow set the parameters that pass, but the other Activity does not rescue.

  • In case it would be the Intent data is void or the Parcelable within it comes null?

  • the Intent is coming vázio

  • 1

    My suggestion would be to use the getIntent instead of instantiating a new and calling setResult(RESULT_OK, intent) instead of setResult(1, intent).

  • I don’t know why but it worked out vlwz

  • Ah, show, I’ll create an answer to leave to other users.

Show 2 more comments

1 answer

1


My suggestion is to use your own getIntent, but I believe it is optional (I saw a code of mine that does different and works :P), instead of creating a new one and set the resultCode as RESULT_OK:

Intent intent = getIntent();

intent.putExtra("objBairro", objBairro);

setResult(RESULT_OK, intent);
finish();

It’s good practice to use the RESULT_OK.

My other suggestion is to actually use the requestCode as the flag to indicate if it is editing or creating.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.