Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onActivityResult is deprecated #26

Closed
zihadrizkyef opened this issue Jan 7, 2021 · 14 comments · Fixed by #145
Closed

onActivityResult is deprecated #26

zihadrizkyef opened this issue Jan 7, 2021 · 14 comments · Fixed by #145

Comments

@zihadrizkyef
Copy link

zihadrizkyef commented Jan 7, 2021

Hi, i really love your project, but i see that we are still using OnActivityResult which is deprecated.
Maybe we can use this one : https://developer.android.com/reference/androidx/activity/result/contract/ActivityResultContract

@Canato Canato added enhancement and removed bug labels Jan 7, 2021
@Canato
Copy link
Member

Canato commented Jan 7, 2021

Hey @zihadrizkyef thanks for the suggestion, for sure is time for us to change.

I'm happy if you have time to do it, just need to open the PR ^^

@Canato Canato changed the title [BUG] - onActivityResult is deprecated onActivityResult is deprecated Jan 7, 2021
@dimiitpk
Copy link

Hello guys, there is a way to do this with ActivityResult Api
Create custom contract :

private val cropResultContract by lazy {
        object : ActivityResultContract<Any?, Uri?>() {
            override fun createIntent(context: Context, input: Any?): Intent {
                return CropImage
                    .activity()
                    .setCropShape(CropImageView.CropShape.RECTANGLE)
                    .getIntent(this@Fragment.requireContext())
            }


            override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
                return CropImage.getActivityResult(intent)?.uri
            }
        }
    }

Create launcher

private lateinit var cropResultLauncher: ActivityResultLauncher<Any?>

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        cropResultLauncher = registerForActivityResult(cropResultContract) { uri ->
            uri?.path?.let {
                // parse Uri
            }
        }
    }

and when u need to start it

cropResultLauncher.launch(null)

@Canato
Copy link
Member

Canato commented Jan 29, 2021

Yes @dimiitpk

And this is something we want to add in the library directly, I'm organising now all work to be done in the lib =p

But feel free to open a PR if you are confident of how doing it!

@AlexanderHoddleITE
Copy link

AlexanderHoddleITE commented May 10, 2021

It is not a bug nor an issue with current version.
@dimiitpk Your idea is nice, but there is a much easier way to do it with ActivityResultContracts.
You need to define:

private var launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
	if (result.resultCode == Activity.RESULT_OK) {
		CropImage.getActivityResult(result.data)?.let { cropResult ->
			// work with cropResult
		}
	}
}

And then launch like this:

val intent = CropImage
	.activity()
	.setCropShape(CropImageView.CropShape.RECTANGLE)
	.getIntent(requireContext())
	
launcher.launch(intent)

@Canato
Copy link
Member

Canato commented May 10, 2021

@AlexanderHoddleITE please feel free to drop a PR to update the library if you are feeling up to help the community today, hahahaha

@ArcherEmiya05
Copy link

ArcherEmiya05 commented May 14, 2021

@Canato no need for PR to update the library for this one, just like @AlexanderHoddleITE said this is not a bug nor an issue with the library as it is already able to work with the latest approach of onActivityResult. Just need to update the README.md for the documentation update. You can close this now.

@Canato
Copy link
Member

Canato commented May 14, 2021

Indeed @ArcherEmiya05 you can do your own wrapper around, but we are still using onActivityResult inside the library, what now is deprecated. Is on my plans to have this wrapper already in the lib, so we could have it without the wrapper =D

@ArcherEmiya05
Copy link

Indeed @ArcherEmiya05 you can do your own wrapper around, but we are still using onActivityResult inside the library, what now is deprecated. Is on my plans to have this wrapper already in the lib, so we could have it without the wrapper =D

Can't it just be remove since all we need is the intent itself then just leave it to consumer to handle the result?

@connyduck
Copy link
Contributor

I would like to send a PR for this, but I am not sure what the best option is here.

One way to have this working could be to have CropImageOptions as input and ActivityResult as output like this:

class CropImageContract : ActivityResultContract<CropImageOptions, CropImage.ActivityResult?>() {

    override fun createIntent(context: Context, input: CropImageOptions): Intent {
        input.validate()
        return Intent().apply {
            val bundle = Bundle()
            bundle.putParcelable(CropImage.CROP_IMAGE_EXTRA_OPTIONS, input)
            putExtra(CropImage.CROP_IMAGE_EXTRA_BUNDLE, bundle)
        }
    }

    override fun parseResult(
        resultCode: Int,
        intent: Intent?
    ): CropImage.ActivityResult? {
        return getActivityResult(intent)
    }
}

The problem with this is that one cannot supply a Uri as input, so it only works if you want to show the file picker.

Maybe its better to have the options in the constructor and the uri as input?

class CropImageContract(
    private val options: CropImageOptions
) : ActivityResultContract<Uri?, CropImage.ActivityResult?>() {
    
    init {
        options.validate()
    }

    override fun createIntent(context: Context, input: Uri?): Intent {
        return Intent().apply {
            val bundle = Bundle()
            bundle.putParcelable(CropImage.CROP_IMAGE_EXTRA_SOURCE, input)
            bundle.putParcelable(CropImage.CROP_IMAGE_EXTRA_OPTIONS, options)
            putExtra(CropImage.CROP_IMAGE_EXTRA_BUNDLE, bundle)
        }
    }

    override fun parseResult(
        resultCode: Int,
        intent: Intent?
    ): CropImage.ActivityResult? {
        return getActivityResult(intent)
    }
}

This is less flexible if one needs multiple crop options.

Thoughts?

@Canato
Copy link
Member

Canato commented May 21, 2021

@connyduck and a new data class, like

data class CropImageContractInput(
    val uri: Uri?,
    val cropImageOptions: CropImageOptions,
}

So we could use class CropImageContract : ActivityResultContract<CropImageContractInput, CropImage.ActivityResult?>()

Would work? 🤔

@connyduck
Copy link
Contributor

Yes that would work, but its cumbersome if you don't want to supply a uri. Maybe it can be improved by adding some convienience functions. I'll try to find a nice solution.

@Morons
Copy link
Contributor

Morons commented May 31, 2021

I am working with

	private val cropActivityResultContract = object : ActivityResultContract<Any?, Uri>() {
		override fun createIntent(context: Context, input: Any?): Intent {
			return CropImage.activity()
				.setGuidelines(CropImageView.Guidelines.ON)
				.getIntent(context)
		}
		
		override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
			return CropImage.getActivityResult(intent)?.uriContent
		}
	}
	
	private lateinit var cropActivityResultLauncher: ActivityResultLauncher<Any?>

In my Button setOnClickListener or clicking the Image to be replaced etc..
cropActivityResultLauncher.launch(null)

@swagatgithub
Copy link

swagatgithub commented Feb 5, 2022

Hey, @dimiitpk
Have You Implemented in the same way?
i did ,but my ActivityResultCallback is not called..and i am using it inside a fragment..

@Canato
Copy link
Member

Canato commented Feb 5, 2022

Hey @swagatgithub not you don't need to implement yourself like Dimiitpk needed on the time he wrote.

You can use the library activity contracts, like is showed on the README and the sample folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants