1

In my case I would like to call the composable from a OneTimeWorkRequest.

As far as I'm aware composables are typically called from Activities with setContent(). However I do not have access to this within the Worker and not sure how to instantiate a 'blank/dummy' activity within the worker.

@Composable
fun Hello() {
    Text(text = "Hello")
}

class MyWorker(private val appContext: Context, private val params: WorkerParameters)
    : CoroutineWorker(appContext,params) {
    
    override suspend fun doWork(): Result {
        Hello() /*<---error here "@Composable invocations can only happen from the context of a @Composable function"*/
        return Result.success()
    }
}
mars8
  • 770
  • 1
  • 11
  • 25
  • Per your [earlier question](https://stackoverflow.com/questions/74248679/best-way-to-replicate-a-canvas-in-glance-widget), is this in the context of generating a bitmap from that composable? In isolation, your question makes very little sense. – CommonsWare Oct 30 '22 at 12:38
  • @CommonsWare correct. I need to start an activity in order to convert composable to bitmap. – mars8 Oct 30 '22 at 13:08
  • You should not need to start an activity in order to convert a composable to a bitmap. Create a `ComposeView` instance via its constructor, size it as needed, and hand it your composable. [This gist](https://gist.github.com/handstandsam/6ecff2f39da72c0b38c07aa80bbb5a2f) shows using that for an overlay displayed by a service, but you should need only a subset of that code. – CommonsWare Oct 30 '22 at 13:19
  • @CommonsWare I have tried following this link, implemented it, added windowManager to address "composeView is not attached to a window" error, added "SYSTEM_ALERT_WINDOW" to manifest to address "permission denied for window type 2038", but it is still not converting my composeView to bitmap. the bitmap is just blank. If you have any further direction then that would be much appreciated. I just want to convert composeView to bitmap. I think the gist is doing much more than this and not quite what i am after. unsure how to extract the needed parts. – mars8 Oct 30 '22 at 17:53
  • @CommonsWare I have created another [question](https://stackoverflow.com/q/74256929/15597975) on how to convert `composeView` to `BItmap` with example. Many thanks for your direction thus far, would really appreciate if you could take a look. – mars8 Oct 30 '22 at 21:39

0 Answers0