dekans

    dekans

    3 years ago
    I posted about Android Runtime Permission implementation. How to make it a simple suspend function:https://geoffreymetais.github.io/code/runtime-permissions/
    s

    Sergey Chelombitko

    3 years ago
    What will happen if activity gets recreated while a permission dialog is shown?
    dekans

    dekans

    3 years ago
    Good practive would be to cancel scope jobs in
    onDestroy
    , then ask again in new instance
    s

    Sergey Chelombitko

    3 years ago
    The dialog will not be dismissed though and when user clicks Allow or Deny nothing will happen or app might crash.
    dekans

    dekans

    3 years ago
    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.
    uhe

    uhe

    3 years ago
    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

    hmole

    3 years ago
    @uhe You can do it with atomic inputs. Like dialog with an EditText and submit button.
    uhe

    uhe

    3 years ago
    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
    Everytime a coroutine can be "interrupted" by Android you're basically fucked, pardon my French
    h

    hmole

    3 years ago
    Save dialog to the current screen state - then reshow it if Activity is recreated?
    uhe

    uhe

    3 years ago
    Sure, but then why would I need a coroutine for that in the first place? I was thinking more of something along these lines:
    class SomePresenter {
        fun onUserClickedRemove() = launch {
            if (view.userConfirmsDeletion()) // suspends until user has made their choice
               delete()
        }
    }
    dekans

    dekans

    3 years ago
    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.
    louiscad

    louiscad

    3 years ago
    uhe

    uhe

    3 years ago
    @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