我有两个片段:一个Activity中的fragmentA和dialogFragmentB。 fragmentB有两个alertDialogs,它从dialogA开始,然后dialogA调用dismiss()并生成dialogB。 fragmentA有方法onActivityResult()和dialogB(来自dialogFragmentB)必须在按钮的点击后发送一些意图结果。但在我发送结果之前,我必须设置目标fragmentA。如何在dialogB的onClick方法中执行此操作?
我想我需要这样的东西:
public void onClick(){
...
dialogB.dismiss();
fragmentA.setTargetFragment(FragmentA, requestCode);
}
但我有一个错误:找不到符号变量FragmentA
谢谢!
答案 0 :(得分:2)
使用变量引用而不是类名:
public void onClick(){
...
dialogB.dismiss();
// note the lowercase "f"
fragmentA.setTargetFragment(fragmentA, requestCode);
}
答案 1 :(得分:2)
我已经通过下一个方式解决了我的问题:
public class DialogFragmentB extends DialogFragment{
...
public Dialog onCreateDialog (Bundle b){
// here i create two dialogs, first dialogA and then it calls dialogB
// finally dialogB has to return his datas and datas from dialogA in fragmentA
...
public void onClick(View v){
...
FragmentManager fm = getFragmentManager();
// these two code's lines resolved my headache --------------
Fragment ft = fm.findFragmentByTag(FragmentA.FRAGMENT_A_TAG);
ft.setTargetFragment(ft, FragmentA.CODE_REQUEST);
// ----------------------------------------------------------
dialogB.dismiss();
sendResult(Activity.RESULT_OK, myData);
}
return dialogB;
}
// And here myData goes in onActivityResult in fragmentA
private void sendResult(int resultCode, MyData myData){
if(getTargetFragment() == null){
return;
}
Intent intent = new Intent();
intent.putExtra(EXTRA_DATE_2, myData);
getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent);
}
}