I'm trying to open the MainActivity of my app when...
# glance
p
I'm trying to open the MainActivity of my app when widget is clicked, so I did this:
Copy code
InfoPanel(
    glanceModifier = glanceModifier.fillMaxSize().appWidgetBackground()
        .clickable { actionStartActivity<MainActivity>() }
)
Is not working, and the log is printing this every time the widget is clicked:
Copy code
app_time_stats: avg=3761.89ms min=3761.89ms max=3761.89ms count=1
logFgsApiBegin: FGS Logger Transaction failed, -129
No piid assigned for invalid/internal port id 15
mixer(0x7789f5d7f8e0) throttle end: throttle time(36)
Maybe someone know why actionStartActivity doesn't work?
p
Check the return type of
actionStartActivity
I think it returns a function that you should pass into clickable. I had a similar problem recently and I think that was what happened to us. (On mobile so sorry if my memory is bad)
c
p
well, finally I disscovered the problem, it simply was I was using clickable{ } instead of clickable( ) and the first one didn't work with actionStartActivity but the second one yes
maybe is what Pearce was trying to say with "I think it returns a function that you should pass into clickable" but I didn't figure it reading that phrase
p
Yeah exactly, when you use () you're passing in the result as the callback, when you use {} you're creating a new callback wrapping the result so you have one extra layer of callback
b
Actions and lambdas are unfortunately a little confusing. Actions are intended for starting activities/services/broadcasts. Actions handle passing pending intents to the remoteviews, and do some other internal logic that makes them efficient for starting components. Importantly, start activity actions do not cause composition to rerun. And if you are starting a component that is not in your app, such as a web browser, they don't even wake your app up, which is good from a battery perspective. Actions are handled like this
Copy code
public fun GlanceModifier.clickable(onClick: Action): GlanceModifier =
    this.then(ActionModifier(onClick))
Lambdas do cause composition to rerun, which is good when you want to update the internal state of your app and produce a new ui state for your widget. This is good if you need to kick off a refresh, save something to persistence, etc without starting an activity. Activities should not be started from lambdas. Lambdas are handled like this
Copy code
@Composable
public fun GlanceModifier.clickable(block: () -> Unit): GlanceModifier =
    this.then(ActionModifier(action(block = block)))