onActivityResult does not return setResult

1

What happens is that I have an Activity Client where I have 2 buttons, one of register and another of query, when I call the register 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 (do not ask me why, my boss asked so), my difficulty is as follows, I'm on the registration screen, I go to the neighborhood and I call the startActivityForResult, if I called the register screen directly from the register button, the SetResult of the Neighborhood Query returns to Intent, if I call the Client Query Activity and I search for a Neighborhood, I do not returns the values of the setResult.

NOTE: I use the same activity to Register and Edit.

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

Within the Neighborhood Query:

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

NOTE: I use value 0 and 1 in an attribute to know when it is registered and when it is edited;

    
asked by anonymous 19.10.2015 / 21:54

1 answer

1

My suggestion is to use getIntent itself, 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 setting resultCode to RESULT_OK :

Intent intent = getIntent();

intent.putExtra("objBairro", objBairro);

setResult(RESULT_OK, intent);
finish();

It's a good practice to use RESULT_OK .

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

    
20.10.2015 / 13:57