https://kotlinlang.org logo
#feed
Title
d

dekans

03/12/2019, 3:20 PM
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

03/12/2019, 3:27 PM
What will happen if activity gets recreated while a permission dialog is shown?
2
d

dekans

03/12/2019, 3:29 PM
Good practive would be to cancel scope jobs in
onDestroy
, then ask again in new instance
s

Sergey Chelombitko

03/12/2019, 3:32 PM
The dialog will not be dismissed though and when user clicks Allow or Deny nothing will happen or app might crash.
d

dekans

03/12/2019, 3:36 PM
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

uhe

03/12/2019, 4:55 PM
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

03/13/2019, 3:29 PM
@uhe You can do it with atomic inputs. Like dialog with an EditText and submit button.
u

uhe

03/13/2019, 3:31 PM
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

hmole

03/13/2019, 3:43 PM
Save dialog to the current screen state - then reshow it if Activity is recreated?
u

uhe

03/13/2019, 3:43 PM
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

dekans

03/13/2019, 4:06 PM
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

louiscad

03/16/2019, 3:08 PM
u

uhe

03/18/2019, 9:13 AM
@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