how get Result from onActivityResult in Fragment?

前端 未结 3 2077
栀梦
栀梦 2021-02-12 17:37

I have used Navigation drawer in each item click i have called Fragments so in one item i have called one Fragment in this fragment i need

相关标签:
3条回答
  • 2021-02-12 18:20

    onActivityResult should be implemented in Activity, this class is your Fragment.
    Apply onActivityResult inside FragmentActivity.

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

    In Activity class:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    
        super.onActivityResult(requestCode,resultCode,data);
    
    }
    

    In Fragment :

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){}
    

    Option 1 :

    If you're calling startActivityForResult() from the fragment then you should call startActivityForResult() not getActivity().startActivityForResult(), as it will result in fragment onActivityResult().

    If you're not sure where you're calling on startActivityForResult() and how you will be calling methods.

    Option 2:

    Since Activity gets the result of onActivityResult(), you will need to override the activity's onActivityResult() and call super.onActivityResult()to propagate to the respective fragment for unhandled results codes or for all.

    If above 2 options do not work, then refer option 3 as it will definitely work.

    Option 3 :

    Explicit call from fragment to onActivityResult function as follows

    In Parent Activity class, override the onActivityResult() method and even override the same in Fragment Class and call as the following code.

    In Activity:

    @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    Fragment fragment = getSupportFragmentManager().findFragmentById("yourFragment"); 
    fragment.onActivityResult(requestCode, resultCode, data); 
    }
    

    In Fragment:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       //in fragment class callback
    }
    
    0 讨论(0)
  • 2021-02-12 18:38

    Replace

    getActivity().startActivityForResult(intent, CAMERA_DATA);
    

    with

    startActivityForResult(intent, CAMERA_DATA);
    
    0 讨论(0)
提交回复
热议问题