onActivityResult DialogFragment不起作用

时间:2013-12-19 15:34:39

标签: android android-intent dialog

我在android中通过fragmentDialog.Befire执行fileupload来测试按钮我做了活动等测试它,evrything工作正常,但是因为我把它放在dilogfragment下没有任何作用:

public class UploadF extends DialogFragment{

    ....

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {


    gallery_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent gal = new Intent();
            gal.setType("image/*");
            gal.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(gal, "Select Picture"), SELECT_IMAGE);

        }

    });




    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult( requestCode,  resultCode,  data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SELECT_IMAGE) {
                ....

                if ( selectedImagePath != null ) {
                    UploadFileI task = new UploadFileI( getActivity());
                    task.execute( url );
                }
            }



        }

    }



}

1 个答案:

答案 0 :(得分:6)

你是对的,没有任何事情发生。 Activity有一个方法onActivityResult,而Fragment也有方法getActivity().startActivityForResult(Intent.createChooser(gal, "Select Picture"), SELECT_IMAGE); 。这可能会令人困惑,为了进步,请尝试通过调用:

来使用Activity上下文
onActivityResult

这样,创建您的UploadF的Activity将收到结果。因此,您必须将Activity移至public class YourActivity extends Activity { ... public void onActivityResult(int requestCode, int resultCode, Intent data){} ... }

{{1}}

在UploadF之外和父级Activity中的功能。 如果这不起作用,请在DialogFragment

中搜索startActivityForResult