0

I'm using RxPermissions to handle permissions inside my project.

 'com.github.tbruyelle:rxpermissions:0.12'
 'io.reactivex.rxjava3:rxjava:3.0.4'

This is my code for handling the CAMERA permission:

new RxPermissions((FragmentActivity) context).request(Manifest.permission.CAMERA)
            .subscribe(granted -> {
                if(granted){
                    Intent packageReceiveIntent = new Intent(context, ReceivePackageActivity.class);
                    context.startActivity(packageReceiveIntent);
                }
            });

This gets called onClick event.

For some reason the code inside the Consumer is not called after the permission is granted and I have to tap on the button twice to open the activity.

I've used the same code in a different part of the application and it works fine, I don't get it why in a Fragment it works fine and in another it doesn't.

I also tried it like this but was unsuccessful:

 new RxPermissions(requireActivity()).request(Manifest.permission.CAMERA)
            .subscribeOn(Schedulers.newThread())
            .observeOn(Schedulers.computation())
            .subscribe(aBoolean -> {
                if (aBoolean) {
                    Intent packageReceiveIntent = new Intent(requireActivity(), ReceivePackageActivity.class);
                    requireActivity().startActivity(packageReceiveIntent);
                }
            });
petrrr33
  • 583
  • 9
  • 24
  • just asking, but do you really want/need another library to handle permissions ? – a_local_nobody Mar 02 '21 at 10:39
  • @a_local_nobody Avoid worrying about the framework version. If the sdk is pre-M, the observer will automatically receive a granted result. Prevents you to split your code between the permission request and the result handling. Currently without this library you have to request the permission in one place and handle the result in Activity.onRequestPermissionsResult() – petrrr33 Mar 02 '21 at 11:29
  • i don't particularly see the advantage to this still but that's irrelevant, i was just asking if you specifically want to use this library or not – a_local_nobody Mar 02 '21 at 11:30

2 Answers2

1

I have the very same problem with RxPermissions. For resolving this I check, if permission is not granted then I ask for permission

Observable.just(isGranted(Manifest.permission.CAMERA))
            .flatMap { granted ->
                if (granted)
                    Observable.just(true)
                else rxPermissions.request(permission)
            }
            .subscribe {

            }


fun checkIsGranted(permission: String): Boolean {
    return ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED;
}

It's written in kotlin, you can change it to java. Happy Coding!

com_run
  • 103
  • 1
  • 2
  • 10
  • This looks promising. I'll try it out. Can you please provide the alternative for `flatMap` in Java? Or the whole snippet if you have the time? Cheers – petrrr33 Mar 02 '21 at 11:46
  • 1
    No problem, Observable.just(checkIsGranted(activity, permission)) .flatMap(isgranted -> { if (isgranted) return Observable.just(true); else return new RxPermissions(activity).request(permission); }).subscribe(); – com_run Mar 02 '21 at 12:20
  • Tried it but no luck, same behaviour. But thanks anyway man – petrrr33 Mar 02 '21 at 12:25
0

The situation I encountered was that the Activity was destroyed, so the callback of RxPermissions was not called.

xyz
  • 100
  • 1
  • 4