xrubioj / JetpackComposeOverlayTest

MIT License
6 stars 0 forks source link

Tests overlays using Jetpack Compose

This is a small test project to explore how to use Jetpack Compose in an overlay.

The problem

The main problem is that there are a few requirements that are usually implemented in the Activity that are not available when you simply instantiate a ComposeView‎ to wrap you Compose code.

I used Sam Edwards work to create the initial scaffolding to solve the problem of the missing lifecycle owner and saved state registry owner (my initial attemps where too naive).

https://www.jetpackcompose.app/snippets/OverlayService

I had to update a few things to make it compile and work with current Jetpack libraries, as Sam's example was for an older version.

Also, more importantly, the lifecycle was transitioned to CREATE, but it was not STARTed nor RESUMEd. Without that, recomposition was not happening.

Code structure

Other considerations

Also, notice that in order for the app to work it requires the user to enable overlays (if you tap the "Open Overlay Activity" button it opens the system settings) and also needs the user to manually enable notifications for the app (the permission is there, but I haven't implemented the code to request it yet).

Finally, currently there's no way to dismiss the overlay. This is just a small PoC. I may implement it later.

UPDATE: the original Gist with comments pointing out to the same solutions I found seems to be here:

https://gist.github.com/handstandsam/6ecff2f39da72c0b38c07aa80bbb5a2f