I posted about Android Runtime Permission implemen...
# feed
d
I posted about Android Runtime Permission implementation. How to make it a simple suspend function: https://geoffreymetais.github.io/code/runtime-permissions/
s
What will happen if activity gets recreated while a permission dialog is shown?
2
d
Good practive would be to cancel scope jobs in
onDestroy
, then ask again in new instance
s
The dialog will not be dismissed though and when user clicks Allow or Deny nothing will happen or app might crash.
d
Permission are a system-side state
app won't crash, and next permission check will return true
But yes, making it a
suspend function
is a bit tricky about activity recreation.
u
It would be sooo nice to be able to use suspending functions to await user input, but alas, Android's view architecture kills that 😞
h
@uhe You can do it with atomic inputs. Like dialog with an EditText and submit button.
u
How? That is still subject to the problems that come with the Android app lifecycle like config changes and the system killing your app in the background
h
Save dialog to the current screen state - then reshow it if Activity is recreated?
u
Everytime a coroutine can be "interrupted" by Android you're basically fucked, pardon my French
Sure, but then why would I need a coroutine for that in the first place? I was thinking more of something along these lines:
Copy code
class SomePresenter {
    fun onUserClickedRemove() = launch {
        if (view.userConfirmsDeletion()) // suspends until user has made their choice
           delete()
    }
}
d
Yep, the coroutine would resume in the old
Activity
instance. In my onboarding activity, the click on 'next' button also checks if perm is already granted. If you grant perm after activity rotation, the old activity doesn't receive it (
scope.cancel()
has been fired) But permission is granted by Android, so the new check returns true
retain instance + LiveData would solve the rotation problem, but not a restore of the app by Android.
l
u
@louiscad I'm afraid what I'm looking for isn't really possible at all, since there is no way of keeping a coroutine alive in all circumstances (like Android killing the process in the background while a dialog view is open)
I have now settled for old-fashioned events that are sent when a dialog is closed, which is way uglier but at least I don't have lifecycle problems anymore