Android onActivityResult从未在Fragment中从DialogFragment调用

时间:2016-03-08 11:18:10

标签: android camera fragment onactivityresult dialogfragment

我希望显示Dialog,以便在Camera中打开Gallery / Fragment之间进行选择。

按下Button后,我会在DialogFragment内显示自定义DialogFragment(此innerclass Fragment

public static class AddPictureDialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_add_picture_dialog, container, false);
        getDialog().setTitle("New Picture");

        Button openCameraButton = (Button) rootView.findViewById(R.id.open_camera_button);
        openCameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("","Open Camera Option Selected");
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, REQUEST_CAMERA);
                getDialog().dismiss();

            }
        });

        Button openGalleryButton = (Button) rootView.findViewById(R.id.open_gallery_button);
        openGalleryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("","Open Gallery Option Selected");
                Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
                getDialog().dismiss();
            }
        });

        return rootView;
    }

}

选择一个opcion后,Camera / Gallery会被初始化,但在制作/选择图片后,方法onActivityResult永远不会被调用。

以下是我创建DialogFragment的代码:

FragmentManager fm = getActivity().getSupportFragmentManager();
    AddPictureDialogFragment addPictureDialogFragment = new AddPictureDialogFragment();
    addPictureDialogFragment.show(fm, getTag());

奇怪的是,如果我直接在DialogFragment中创建Fragment而不使用DialogFragment就可以了...

在创建DialogFragment

时,我也尝试了这一点
addPictureDialogFragment.setTargetFragment(this, 1);

这样,在初始化camera Intent

Button openCameraButton = (Button) rootView.findViewById(R.id.open_camera_button);
    openCameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("","Open Camera Option Selected");
            getDialog().dismiss();

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            getTargetFragment().onActivityResult(getTargetRequestCode(), 1, intent); 
            startActivityForResult(intent, REQUEST_CAMERA);         
          }
    });

5 个答案:

答案 0 :(得分:3)

试试这个:

function placeVoteForCandidate()
{
    global $connect;

    $username = $_POST["username"];

    $query = "UPDATE User SET votesAttained=votesAttained+1 WHERE USER_NAME = $username";

    mysqli_query($connect, $query) or die (mysqli_error($connect));
    mysqli_close($connect);

    $message['success'] = 'Vote added';
    echo json_encode($message);
}

答案 1 :(得分:2)

在您的主活动中覆盖onActivityResult方法,如下所示。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
     if(fragment!=null)
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

有时片段可能会被破坏,但引用可能会持续存在,因此if(片段!= null)检查。

答案 2 :(得分:2)

请在您的主要活动中写下此代码。

 // In your activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("onActivityResult Main Activity");
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

答案 3 :(得分:2)

这部分很简单

致电startActivityForResult(intent,constant); 在你的片段类

覆盖活动中的onActivityResult,不要删除super.onActivityResult(requestCode, resultCode, data);

还会覆盖片段类

中的onActivityResult

活动

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "activity result called");
    super.onActivityResult(requestCode, resultCode, data);
}

片段

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "fragment result called");
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==constant){
        Logger.d(TAG, "got result");
    }
}

答案 4 :(得分:1)

覆盖父Activity中的onActivityResult,即所有片段的父级

在您的主要活动上覆盖此方法:

jQuery(document).on('ready',function () {
    if ($(".Video_Pane iframe").length && $(".Image_Pane img").length) {
        $(".Video_Pane iframe").height($(".Image_Pane img").height())
    }
});
片段活动结果调用后