How to use onActivityResult method from other than Activity class

前端 未结 7 1199
鱼传尺愫
鱼传尺愫 2020-12-05 23:53

I am creating an app where i need to find current location of user .

So here I would like to do a task like when user returns from that System intent, my task shou

7条回答
  •  不思量自难忘°
    2020-12-06 00:39

    I am using it like this this may be helpful to others

    In my fragment I have

    // Upload Cover Photo On Button Click
    btn.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
    
            // Start The Image Cropper And Go To onActivityResult
            Intent intent = ImageManager.startImageCropper(getContext());
            startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
    
        }
    });
    

    Then Calling The Result Like This In The Fragment

    // On Activity Result for Start Activity For Result
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        super.onActivityResult(requestCode, resultCode, data);
    
        // Get The Image From Image Cropper
        Uri resultUri = ImageManager.activityResult(requestCode, resultCode, data, getContext());
    }
    

    The public class / functions supporting these are

    public class ImageManager {
    
        // Start Image Cropper
        public static Intent startImageCropper(Context context) {
    
            // Crop Image
            Intent intent = CropImage.activity()
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .setActivityTitle("Title")
                    .setCropMenuCropButtonTitle("Save")
                    .setAutoZoomEnabled(true)
                    .setAspectRatio(1, 1)
                    .getIntent(context);
    
            return intent;
    
        }
    
        public static Uri activityResult(int requestCode, int resultCode, Intent data, Context context) {
    
            // Handle Cropped Image
    
            Uri resultUri = null;
    
            if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
                if (resultCode == Activity.RESULT_OK) {
                    resultUri = result.getUri();
    
                } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                    Exception error = result.getError();
                    Toast.makeText(context, (CharSequence) error, Toast.LENGTH_SHORT).show();
                }
    
            }
            return resultUri;
        }
    }
    

提交回复
热议问题