Search code examples
androidandroid-fragmentsandroid-activityonactivityresult

onActivityResult returns intent data = null when calling the second time


I know there're answers about this topic but the behaviour isn't the same as the other questions.

I have a fragment that has two buttons with two differente onClick methods. Each onClick method launch an startActivityForResult() but on the onActivityResult(int requestCode, int resultCode, Intent data) method in the fragment only one of those activities have the resultCode correct.

Let me explain it with code and a example:

Fragment:

@Override
public void onClick(View view) {
    int id = view.getId();
    switch (id) {

        case R.id.fab:
            Log.e(LOG_TAG, getActivity().toString());
            Intent intent = new Intent(getActivity(), AnadirAlimentoActivity.class);
            startActivityForResult(intent, 2);
            break;

    }
}

this AnadirAlimentoActivity is the one that works exactly as expected.

AnadirAlimentoActivity:

When you have ended of adding items to the list you can click on the back button and that fires this method:

    @Override
public void onBackPressed() {
    Intent intent = new Intent();
    intent.putExtra(AppConstant.LISTA_ALIMENTOS_SELECCIONADOS, alimentosSeleccionados);
    setResult(RESULT_OK, intent);
    finish();
}

And now in the fragment the OnActivityResult method gets fired with the correct resultCode, requestCode and data

Fragment:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 2) {
        ArrayList<Alimento> alimentos = data.getParcelableArrayListExtra(AppConstant.LISTA_ALIMENTOS_SELECCIONADOS);
        if (alimentos != null) {
            for (Alimento alimento : alimentos) {
                Log.e("alimento devuelto", alimento.getDescripcion() + " cantidad: " + String.valueOf(alimento.getCantidad())
                        + " proc: " + alimento.getProcedencia());
                mAlimentos.add(alimento);
            }
        }
        popularLista();
        isEnviarAlimento();
    } else {
        if (requestCode == 3) {
            if (data != null) {
                String remanente = data.getStringExtra(AppConstant.INSULINA_REMANENTE);
                if (remanente != null) {
                    _insulinaRemanente.setText(remanente);

                }
            }
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

Then problem is that when you click on the other button, the one that launch the other activity (is in the fragment too but within a AsyncTask onPostExecute Method)

Fragment (inside this fragment class there is a private class extends AsyncTask)

        @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        if (!aBoolean) {
            Toast.makeText(getActivity().getApplicationContext(), "Ha ocurrido un error por favor vuelve a intentarlo", Toast.LENGTH_LONG).show();
        } else {
            Intent intent = new Intent(getActivity(), MostrarInsulinaActivity.class);
            Log.e(LOG_TAG, getActivity().toString());
            startActivityForResult(intent, 3);
        }
        if (progressDialog != null && progressDialog.isShowing())
            progressDialog.dismiss();
    }

And in

MostrarInsulinaActivity

when you press on the backButton again

MostrarInsulinaActivity:

    @Override
public void onBackPressed() {
    super.onBackPressed();
    Intent _intent = new Intent();
    _intent.putExtra(AppConstant.INSULINA_REMANENTE, "holoa");
    setResult(RESULT_OK, _intent);
    finish();

}

I'm doing the same as in the other class expecting to behave exactly as the previous one but after the finish() is get calls the OnActitivyResult method in the fragment with the correct requestCode but with resultCode == 0 and the data intent = null.

Why is this happening? is because the AsynTask?

I'm a little bit lost and I don't know where to go from here.

Thank you so much


Solution

  • I want to answer my own question just for someone who might be facing the same issue..

    The thing was that on my second activity I was calling super.onBackPressed(); before setting my intent so this is the change that I made:

    @Override
    public void onBackPressed() {
      Intent _intent = new Intent();
      _intent.putExtra(AppConstant.INSULINA_REMANENTE, "holoa");
      setResult(RESULT_OK, _intent);
      finish();
      super.onBackPressed();
    }