从RecyclerView.Adapter内部调用的Activity的Fragment上获取onActivityResult [英] get onActivityResult on Fragment from Activity that called inside RecyclerView.Adapter
问题描述
好的,这是我的问题
- 我有一个片段,其中包含RecyclerView,当然还有一个容纳内容的适配器(称为
ApprovalCutiCardAdapter
). -
在
ApprovalCutiCardAdapter
里面,我在卡上设置了OnClickListener,当单击Card时,它将启动一个名为DetailApprovalCuti
的活动.这是我启动活动的代码
- I have a Fragment that contains RecyclerView, and of course an adapter (called
ApprovalCutiCardAdapter
) that hold the content. Inside that
ApprovalCutiCardAdapter
I set OnClickListener on the card, when Card is clicked it will launch an Activity calledDetailApprovalCuti
. 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屋!