从RecyclerView.Adapter内部调用的Activity的Fragment上获取onActivityResult [英] get onActivityResult on Fragment from Activity that called inside RecyclerView.Adapter

查看:707
本文介绍了从RecyclerView.Adapter内部调用的Activity的Fragment上获取onActivityResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我的问题

  1. 我有一个片段,其中包含RecyclerView,当然还有一个容纳内容的适配器(称为ApprovalCutiCardAdapter).
  2. ApprovalCutiCardAdapter里面,我在卡上设置了OnClickListener,当单击Card时,它将启动一个名为DetailApprovalCuti的活动.这是我启动活动的代码

  1. I have a Fragment that contains RecyclerView, and of course an adapter (called ApprovalCutiCardAdapter) that hold the content.
  2. Inside that ApprovalCutiCardAdapter I set OnClickListener on the card, when Card is clicked it will launch an Activity called DetailApprovalCuti. Here is my code to launch the activity

((Activity) MyApplication.getmContext()).startActivityForResult(detailApprovalCutiIntent, 1);

  • DetailApprovalCuti中,我正在执行finishActivity(1)以获得onActivityResult事件.但是,并不是在所有地方都调用该事件(在承载该片段的活动中以及在该片段中)

  • In DetailApprovalCuti I'm executing finishActivity(1) to get an event of onActivityResult. But that event is not being called everywhere (in Activity that host the fragment, and in the fragment)

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("result", "ApprovalIzinCutiFragment");
        super.onActivityResult(requestCode, resultCode, data);
    }
    

  • 这是我启动Acvitity的代码

  • Here's my code to start the Acvitity

    @Override
    public void onBindViewHolder(final CutiViewHolder holder, final int position) {
    ....
    holder.cv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent detailApprovalCutiIntent = new Intent(MyApplication.getmContext(), DetailApprovalCuti.class);
            Bundle b = new Bundle();
            b.putParcelable("cuti", ApprovalCutiCardAdapter.allCuti.get(position));
            b.putParcelable("process_cuti", ApprovalCutiCardAdapter.allCuti.get(position).getProcessCuti());
            detailApprovalCutiIntent.putExtras(b);
            ((Activity)MyApplication.getmContext()).startActivityForResult(detailApprovalCutiIntent,1);
        }
    });
    
    ....
    }
    

  • 这是我完成活动的代码

  • Here's my code to finish the activity

    btnReject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new AlertDialog.Builder(DetailApprovalCuti.this)
                    .setTitle("Peringatan")
                    .setMessage("Apakah anda yakin?")
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            setResult(1);
                            finish();
                        }
                    }).setNegativeButton(android.R.string.no, null).show();
        }
    });
    

  • 推荐答案

    问题在于,在下面一行中,您正在从一个Activity中调用startActivityForResult(),而这可能不是您所期望的.

    The problem is that, in the following line, you're calling startActivityForResult() from an Activity which might not be the one you're expecting.

    ((Activity)MyApplication.getmContext()).startActivityForResult(detailApprovalCutiIntent, 1);
    

    考虑到适配器是从Fragment设置的,您应该将上面的行修改为:

    Considering that your adapter is set from a Fragment, you should modify the above line to:

    fragment.getActivity().startActivityForResult(detailApprovalCutiIntent, 1);
    

    经验教训

    对于信物是病态的骗子,请不要使用单身信徒.

    PS:单个实例很好,但单例(如全局变量)是一个糟糕的设计,必须避免.

    PS: single instances are fine but singletons (like global variables) are a bad design and must be avoided.

    这篇关于从RecyclerView.Adapter内部调用的Activity的Fragment上获取onActivityResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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