Fragment onActivityResult method on executing calls activity onActivityResult

前端 未结 5 1355
余生分开走
余生分开走 2021-02-09 17:57

In my fragment I have started startActivityforresult intent for photo capture.I have overridden onActivityResult callback method in fragment class. I have implemented onActivity

相关标签:
5条回答
  • 2021-02-09 18:08

    Try remove the line super.onActivityResult(requestCode, resultCode, data); in your fragment onActivityResult().

    0 讨论(0)
  • 2021-02-09 18:12

    The onActivityResult in Activity will be called first, you should not remove processing the result in Activity but do what you want via checking the requestCode.

    0 讨论(0)
  • 2021-02-09 18:13

    If you have onActivityResult defined in your Activity, you can't skip it and go directly to the Fragment. You can however redirect it to the Fragment if the Activity does not know how to handle it. Use unique requestCodes to differentiate between who handles the result.

    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        boolean processed = true;
    
        if (resultCode == Activity.RESULT_OK) 
        {
            if (requestCode == 0) {
                // Something
            } else if (requestCode == 1) {
                // Something
            } else {
                processed = false;
            }
        } else { // Error
            if (requestCode == 0) {
                // Handle error 
            } else {
                processed = false;
            }
        }
    
        if (!processed) {
            fragment1.onActivityResult(requestCode, resultCode, data);
            fragment2.onActivityResult(requestCode, resultCode, data);
            ...
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
    

    Note: Make sure to call getActivity().startActivityForResult() from your fragment and not simply using this.startActivityForResult()

    public void something(Intent intent) {
        getActivity().startActivityForResult(intent);
        // or if you are using SherlockActionBar/Support package
        getSupportActivity().startActivityForResult(intent);
    }
    

    Hope this helps.

    0 讨论(0)
  • 2021-02-09 18:19

    public void onActivityResult is called when the Activity you started with startActivityForResult is finished calling finish()

    0 讨论(0)
  • 2021-02-09 18:21

    I aslo face this problem.

    Calling of an activity from fragment

    ((Activity)context).startActivityForResult(photoPickerIntent, 100);

    here "context" from FragmentActivity. Now the result comes in FargmentActivity onActivityResult method. to switch this result you have to declare a static variable of fragment class in fragmetn activity class.

    static ActivityUploadPhotos activityUploadPhotos;
    
    ...
    ActivityUploadPhotos f = new ActivityUploadPhotos();
    ActivityHome.activityUploadPhotos=f;
    ......
    
    if(activityUploadPhotos!=null)
    {
               activityUploadPhotos.onActivityResult(requestCode, resultCode, data);
    }
    
    0 讨论(0)
提交回复
热议问题