Why doesn't onActivityResult() works when I cl

2019-09-06 13:26发布

问题:

I'm working with an Activity where it shows a AlertDialogFragment with two options, each one opens a new Activity, when I choose the first option I use this code:

switch (which){
    case 0:
        //Intent para registrar una compra
        Intent compra=new Intent(getActivity(),CompraActivity.class);
        compra.putExtra("NumeroTarjeta", tarjetasCredito.getNumeroTarjeta());
        compra.putExtra("SaldoDisponible",tarjetasCredito.getSaldoDisponible());
        startActivityForResult(compra, RESULT_CORRECT);
        break;
    case 1:
        //Intent para registrar pago
        Intent pago=new Intent(getActivity(),PagoActivity.class);
        pago.putExtra("NumeroTarjeta", tarjetasCredito.getNumeroTarjeta());
        startActivityForResult(pago, RESULT_CORRECT);
        break;
}

RESULT_CORRECT = 1

This works fine, but the problem happens when I set the result in the second activity, the onActivityResult(), appears to not work.

what I want to do is launch a method that changes the data of a ListView. What could be the problem? I've already tried some things but nothing works as expected.

Here is the code from the second activity:

public void AgregarCompra(View view) {
    try{
        String lugarCompra=lugar.getText().toString();
        double totalCompra=Double.valueOf(total.getText().toString());
        if(isDataCorrect(lugarCompra,totalCompra)){
            if(SaldoDisponible >= totalCompra){
                DB.SQLFields="NumeroTarjeta,Lugar,Total,Fecha,TipoMovimiento";
                DB.SQLInsert="INSERT INTO "+TABLAS.HISTORIAL_CREDITO+"("+DB.SQLFields+") VALUES ('"+NumeroTarjeta+"','"+lugarCompra+"',"+totalCompra+",'"+ fechaCompra +"','Compra'"+");";
                DB.db.execSQL(DB.SQLInsert);
                DB.SQLUpdate="UPDATE "+TABLAS.TARJETAS_CREDITO+" SET SaldoDisponible="+(SaldoDisponible-totalCompra)+"";
                DB.db.execSQL(DB.SQLUpdate);
                DB.db.close();
                Toast.makeText(this,"Exito al guardar.",Toast.LENGTH_SHORT).show();
                Intent returnIntent=new Intent();
                setResult(RESULT_OK,returnIntent);
                finish();
            }else{
                ErrorDialogFragment errorDialog=new ErrorDialogFragment();
                errorDialog.show(getFragmentManager(),"Error");
            }
        }else{
            Toast.makeText(this,"Datos vacios.",Toast.LENGTH_SHORT).show();
        }
    }catch (Exception ex){
        Log.e(LOG_TAG,"Error de SQLite: "+ex.getMessage());
        Toast.makeText(this,"Error al insertar.",Toast.LENGTH_SHORT).show();
    }
}

As you can see, I already send the result in the method before I finished it. This is the override method in my first activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==RESULT_CORRECT ){
        if(resultCode==RESULT_OK){
            RefreshList();
            mAdapter.notifyDataSetChanged();
        }
    }else{
    }
}

Also, is weird that even in the logcat, there's no problem or some error, everything works good for exception of the method when the second activity closes.

回答1:

Use this

getActivity().startActivityForResult(compra, RESULT_CORRECT);

and

getActivity().startActivityForResult(pago, RESULT_CORRECT);

using getActivity() ensures that onActivityResult() of the activity will be called not the dialog fragment's.

When you don't use getActivity() then onActivityResult of the fragment is called. Because your fragment is a dialog fragment it will be closed as soon as you click the button on it. So you need to handle the result in calling activity. This is achieved by using getActivity().