1

One は normal extends Fragment で、Second は DialogFragment を拡張するといった 2 つのフラグメント間のデモ アプリケーション FragmentActivity を作成します。

最初のフラグメントはダイアログフラグメントを開き、[OK] を押してからそのメソッドにアクセスするため、最初のフラグメントの onActivityResult() メソッドでは処理しません。

親切に私を助けてください

最初のフラグメント コード、

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);
    }
}
4

1 に答える 1

3

私は答えMYSELFを見つけました、

これが、フラグメントとダイアログフラグメントの間の通信を処理する方法です。

MyListFragment コード、

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

このコードは私たちに役立つことを願っています。

于 2013-10-28T10:02:10.527 に答える