Android Question CheckAndRequest Error

cdsincfl

Member
Licensed User
Longtime User
I am updating an app to Android 11 and have raised the targetSdkVersion="28".
My app compiles and runs but I now need to deal with requesting access to File.DirRootExternal so the App can access the secondary storage.

I have used rp.GetSafeDirDefaultExternal but that returns a path to DefaultExternal not DirRootExternal.

I have read and viewed the video for RuntimePermission CheckAndRequest and get an error when I add it to my App.
I used the following code for testing:
B4X:
    rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
    wait for Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        Log("Got Permission")
    Else
        Log("No Permission")
    End If

If I place this code in Activity_Create I get an error that my B4XViews button is not initialized when I attempt to set the button text later in the App.
If I place the code in Activity_Resume I do not get the error and the app compiles and runs.

I need to get permission to access DirRootExternal before Activity_Resume since the App reads and writes data to the External storage when the app starts. I currently have the code to read and write the data to External storage in the Starter Service but I need to check and get permission before that. I can move the code back to the Activity_Create but I get the error if I put the CheckAndRequest in Activity_Create.

The App runs fine except I don't know where to place the CheckAndRequest so I can get the permissions before I read and write to the DirRootExternal location without getting the error.
 

agraham

Expert
Licensed User
Longtime User
Remember that Wait For returns from the Sub containing it immediately and the Sub is re-entered later when the Wait expires. What is probably happening here is that when the Wait For returns from Activity_Create then Activity_Resume is run immediately. the app is probably erroring because you have initialisation code after the Wait For in Activity_Create that something in Activity_Resume relies on.
 
Upvote 0
Top