为什么不工作的onActivityResult当我关闭第二个活动? [英] Why doesn't onActivityResult works when I close a second Activity?

查看:134
本文介绍了为什么不工作的onActivityResult当我关闭第二个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与它显示了两个选项的AlertDialogFragment一个活动的工作,每一个打开一个新的活动,当我选择第一个选项我用这个code:

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
这工作得很好,但是当我在第二次活动设置结果发生的问题,方法的onActivityResult,似乎不工作,我想要做的就是推出改变ListView的数据的方法。
可能是什么问题?我已经尝试了一些东西,但没有按预期工作。
下面是从第二个活动code:

RESULT_CORRECT = 1 This works fine, but the problem happens when I set the result in the second activity, the method onActivityResult, appears to not work, and 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{
    }
}

此外,奇怪的是,即使在logcat中,有没有问题,或者某些错误,一切都运行良好的方法异常时,第二项活动将关闭。

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.

请帮助我。
我在Android Studio中1.0工作

Please help me. I'm working in Android Studio 1.0

推荐答案

使用此

getActivity().startActivityForResult(compra, RESULT_CORRECT);

getActivity().startActivityForResult(pago, RESULT_CORRECT);

使用 getActivity()确保的onActivityResult()活动的将被称为不是对话片段的。

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

当你不使用 getActivity()然后的onActivityResult 片段中被调用。因为你的片段是一个对话框片段作为你点击它的按钮,它会立即关闭。所以,你需要处理调用活动的结果。这是通过使用实现 getActivity()

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().

这篇关于为什么不工作的onActivityResult当我关闭第二个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆