Method onRequestPermissionsResult not called in no app module

278 Views Asked by At

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>
2

There are 2 best solutions below

0
AudioBubble On

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();
0
Anthony Kharin On

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