我在两个片段之间制作演示应用程序FragmentActivity,例如One正常扩展Fragment,Second则扩展DialogFragment。
我不会处理First片段中的onActivityResult()方法,因为第一个片段打开对话框片段然后按下然后访问该方法。
请帮助我 谢谢你的进步
First Fragment Code,
public class MyListFragment extends Fragment {
View mView;
protected static final int REQ_CODE = 1010;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.list_fragment, container, false);
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQ_CODE) {
// Access here
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
答案 0 :(得分:3)
我找到了答案 MYSELF ,
这就是我处理片段和对话片段之间的通信的方式,
MyListFragment Code,
public class MyListFragment extends Fragment {
View mView;
protected static final int REQ_CODE = 1010;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.list_fragment, container, false);
Button mButton = (Button) mView.findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyDialogFragment dialog = new MyDialogFragment();
dialog.setTargetFragment(MyListFragment.this, REQ_CODE);
dialog.show(getFragmentManager(), "dialog");
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQ_CODE) {
// Access here
Log.i(TAG, "onActivityResult Access here method");
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
DialogFragment代码,
public class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("My dialog message")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
notifyToTarget(Activity.RESULT_OK);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
notifyToTarget(Activity.RESULT_CANCELED);
}
});
return builder.create();
}
private void notifyToTarget(int code) {
Fragment targetFragment = getTargetFragment();
if (targetFragment != null) {
targetFragment.onActivityResult(getTargetRequestCode(), code, null);
}
}
}
这段代码我希望对我们有所帮助。