0

I have an activity in no 'app' module where I should use WRITE_EXTERNAL_STORAGE permission. Method onRequestPermissionsResult not called in this module. My code to start this runtime permission

 private fun requestPermissions() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        viewModel?.initTree()
    } else {
        val storagePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE
        val hasPermission = checkSelfPermission(storagePermission)
        val permissions = arrayOf(storagePermission)
        if (hasPermission != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(permissions, PERMISSION_WRITE_EXTERNAL_STORAGE_REQUEST_CODE)
        } else {
            viewModel?.initTree()
        }
    }
}

And onRequestPermissionsResult

override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<String>,
    grantResults: IntArray
) {
    if (requestCode == PERMISSION_WRITE_EXTERNAL_STORAGE_REQUEST_CODE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            viewModel?.initTree()
        } else {
            Toast.makeText(this, R.string.permission_required, Toast.LENGTH_SHORT).show()
        }
    }
}

I have tried to use method ActivityCompat.requestPermissions() and add call super.onRequestPermissionsResult() but it haven't help me. Then I tried to add my code to activity in app module and it works properly.

What the reason of my trouble?

UPD: AndroidManifest.xml in no 'app' module

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.anthony_kharin.file_manager">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application android:requestLegacyExternalStorage="true">
    <activity
        android:name=".view.FileExplorerActivity"
        android:theme="@style/Theme.FileManagerLibrary" />
</application>
  • check [this](https://medium.com/@ajinkya.kolkhede1/requesting-runtime-permissions-using-new-activityresult-api-cb6116551f00) – Sinner of the System Jan 07 '21 at 20:29
  • @SinneroftheSystem I tried this solution right now. Absolutely the same problem. – Anthony Kharin Jan 07 '21 at 21:14
  • Do you see the request happening on the screen? This code looks suspect: `Build.VERSION.SDK_INT < Build.VERSION_CODES.M` what sdk level is your device on? – JensV Jan 08 '21 at 10:29
  • The `onRequestPermissionResult` is only called for the activity where `requestPermissions` is called from. Also, you can only call `requestPermissions` from the current visible activity. – JensV Jan 08 '21 at 10:33
  • Also add some logging statements before and after any `if` conditions and look at the logcat. There should be some message if something goes wrong – JensV Jan 08 '21 at 10:35
  • @JensV On my device, the API level is 29. I have already added logs (this is how I found out that the method is not called). Absolutely the same code, but in the **app** module calls a callback 'onRequestPermissionResult' (also checked with logs). I see the request on the screen and it works, I just can't process the response to it. – Anthony Kharin Jan 08 '21 at 11:03
  • Is the `onRequestPermissionResults` code in the currently visible activity and if it's a superclass, make sure it's not overriden somewhere else. – JensV Jan 08 '21 at 11:15
  • @JensV I started permission in `onCreate` method. Moving logic into `onStart` helped me. Thank you a lot! But this is a bit strange, because in the 'app' module I called also in onCreate and it worked. – Anthony Kharin Jan 08 '21 at 11:31
  • That is weird indeed... perhaps you did some UI related work after the permission request in onCreate. If you request permissions, do this as a last step in onCreate and continue work in onPermissionsResult (unless it's already granted of course) – JensV Jan 08 '21 at 11:34
  • @JensV no, start permissions was last step in `onCreate`. Now I have really strange behavior. `onRequestPermissionResult` called only if I start permission in both methods `onCreate` and `onStart` and it process only first (that closes immediatly because already started second request). – Anthony Kharin Jan 08 '21 at 12:07

2 Answers2

0

Try following source code using dexter library

Dexter.withContext(this)
.withPermission(Manifest.permission.CAMERA)
.withListener(new PermissionListener() {
    @Override public void onPermissionGranted(PermissionGrantedResponse response) {/* ... */}
    @Override public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */}
    @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
}).check();
piotrek1543
  • 19,130
  • 7
  • 81
  • 94
0

The problem was due to complex logic, I closed the activity before onResume.