Search code examples
androidandroid-fragmentsnativeonactivityresult

get onActivityResult on Fragment from Activity that called inside RecyclerView.Adapter


okay here is my problem

  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);
    
  3. 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);
    }
    
  4. 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);
        }
    });
    
    ....
    }
    
  5. 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();
        }
    });
    

Solution

  • 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);
    

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

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

    Lesson learnt

    Never use singletons, for Singletons are Pathological Liars.

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