Search code examples
androidandroid-fragmentsandroid-activityonactivityresult

How to manage onActivityResult() while using fragment in android


I have an application with one activity and several fragments.I have a method in fragment that call onactivityresult.when onactivityresult called I need a variable that exists in fragment not in activity,so how can manage onactivityresult in fragment that can manage it and access to that varible?can I call onactivityresult in fragment instead of activity?

I want to call this in fragment :

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(requestCode==1001){
            iabHelper.handleActivityResult(requestCode,resultCode,data)
        }else{
            super.onActivityResult(requestCode, resultCode, data)
        }
    }

the variable that I need it here is iabHelper


Solution

  • i used onActivityResult in fragment in my project to upload images . i hope it will help you .

      @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
    
                try {
                    Bitmap thumbnail = MediaStore.Images.Media.getBitmap(
                            getActivity().getContentResolver(), imageUri);
                    Bitmap converetdImage = getResizedBitmap(thumbnail, 700);
                    uploadedImagesList.add(converetdImage);
                    uploadImagesAdapter.notifyDataSetChanged();
    
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
            }
        }