Android – onRequestPermissionsResult() is deprecated. Are there any alternatives?

We Are Going To Discuss About Android – onRequestPermissionsResult() is deprecated. Are there any alternatives?. So lets Start this Java Article.

Android – onRequestPermissionsResult() is deprecated. Are there any alternatives?

  1. Android – onRequestPermissionsResult() is deprecated. Are there any alternatives?

    onRequestPermissionsResult() method is deprecated in androidx.fragment.app.Fragment.
    So you use registerForActivityResult() method instead onRequestPermissionsResult().
    You can refer this URL.
    Following is kotlin code. but you can refer it.
    val permReqLuncher = registerForActivityResult(ActivityResultContracts.RequestPermission()){ if (it) { // Good pass } else { // Failed pass } }

  2. onRequestPermissionsResult() is deprecated. Are there any alternatives?

    onRequestPermissionsResult() method is deprecated in androidx.fragment.app.Fragment.
    So you use registerForActivityResult() method instead onRequestPermissionsResult().
    You can refer this URL.
    Following is kotlin code. but you can refer it.
    val permReqLuncher = registerForActivityResult(ActivityResultContracts.RequestPermission()){ if (it) { // Good pass } else { // Failed pass } }

Solution 1

onRequestPermissionsResult() method is deprecated in androidx.fragment.app.Fragment.

So you use registerForActivityResult() method instead onRequestPermissionsResult().

You can refer this URL.

Following is kotlin code. but you can refer it.

val permReqLuncher = registerForActivityResult(ActivityResultContracts.RequestPermission()){
  if (it) {
     // Good pass
  } else {
     // Failed pass
  }
}

I added java code from following URL.
How to get a permission request in new ActivityResult API (1.3.0-alpha05)?

private ActivityResultLauncher<String> mPermissionResult = registerForActivityResult(
        new ActivityResultContracts.RequestPermission(),
        new ActivityResultCallback<Boolean>() {
            @Override
            public void onActivityResult(Boolean result) {
                if(result) {
                    Log.e(TAG, "onActivityResult: PERMISSION GRANTED");
                } else {
                    Log.e(TAG, "onActivityResult: PERMISSION DENIED");
                }
            }
        });



        // Launch the permission window -- this is in onCreateView()
    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         mPermissionResult.launch(Manifest.permission.ACCESS_BACKGROUND_LOCATION);

        }
    });

You can request multiple permissions.

    val requestMultiplePermissions = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
        permissions.entries.forEach {
            Log.e("DEBUG", "${it.key} = ${it.value}")
        }
    }

    requestMultiplePermissions.launch(
        arrayOf(
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.ACCESS_FINE_LOCATION
       )
    )

Original Author Of This Content

Solution 2

A simple way in Kotlin

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.fragment.app.Fragment

class MyFragment : Fragment() {

companion object {
    val TAG: String = MyFragment::class.java.simpleName
    var PERMISSIONS = arrayOf(
        Manifest.permission.CAMERA,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
    )
}

private val permReqLauncher =
    registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
        val granted = permissions.entries.all {
            it.value == true
        }
        if (granted) {
            displayCameraFragment()
        }
    }

private fun takePhoto() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        displayCameraFragment()
    }
    activity?.let {
        if (hasPermissions(activity as Context, PERMISSIONS)) {
            displayCameraFragment()
        } else {
            permReqLauncher.launch(
                PERMISSIONS
            )
        }
    }
}

// util method
private fun hasPermissions(context: Context, permissions: Array<String>): Boolean = permissions.all {
    ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}

private fun displayCameraFragment() {
    // open camera fragment
}
}

Original Author Of This Content

Solution 3

Alternative registerForActivityResult

Example use:

 private fun permissionSetup() {
    val permission = ContextCompat.checkSelfPermission(
        requireContext(), Manifest.permission.READ_CONTACTS)

    if (permission != PackageManager.PERMISSION_GRANTED) {
        permissionsResultCallback.launch(Manifest.permission.READ_CONTACTS)
    } else {
        println("Permission isGranted")
    }
}

private val permissionsResultCallback = registerForActivityResult(
    ActivityResultContracts.RequestPermission()){
    when (it) {
        true -> { println("Permission has been granted by user") }
        false -> {
            Toast.makeText(requireContext(), "Permission denied", Toast.LENGTH_SHORT).show()
            dialog.dismiss()
        }
    }
}

Original Author Of This Content

Solution 4

This works for me – (kotlin):

private fun checkPermissions() {
    if (mContext?.let {
        ContextCompat.checkSelfPermission(
            it,
            READ_EXTERNAL_STORAGE
        )
    } != PackageManager.PERMISSION_GRANTED) {
    Log.d(TAG, "Request Permissions")
    requestMultiplePermissions.launch(
        arrayOf(READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE))
 } else {
        Log.d(TAG, "Permission Already Granted")
    }
}

private val requestMultiplePermissions =
    registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
        permissions.entries.forEach {
            Log.d(TAG, "${it.key} = ${it.value}")
        }
        if (permissions[READ_EXTERNAL_STORAGE] == true && permissions[WRITE_EXTERNAL_STORAGE] == true) {
            Log.d(TAG, "Permission granted")
        } else {
            Log.d(TAG, "Permission not granted")
        }
    }

Original Author Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment