Skip to content Skip to sidebar Skip to footer

The Android Application Crash

WhoDoctor was helped me on the Previous code Now, the android application crashed when during running on my phone and This is the error log First problem is after I scanned the QR

Solution 1:

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

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

Following is kotlin code. .

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

How to get a permission request in new ActivityResult API (1.3.0-alpha05)?

privateActivityResultLauncher<String> mPermissionResult = registerForActivityResult(
        newActivityResultContracts.RequestPermission(),
        newActivityResultCallback<Boolean>() {
            @OverridepublicvoidonActivityResult(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(newView.OnClickListener() {
        @OverridepublicvoidonClick(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
   )
)

Post a Comment for "The Android Application Crash"