https://kotlinlang.org logo
Title
p

Piotr Prus

03/10/2022, 10:25 AM
I have a problem with passing parameters in action
actionStartActivity
. The click parameter is assign during composition and I cannot use hte dynamic state as I can for ActionCallback. Rephrasing: Is there a way to start activity but using class
ActionCallback
? This class is called on click, not during composition, so i can get my parameters base on glanceID. @Marcel Pinto?
d

daivid

03/10/2022, 2:43 PM
yeah, it should work
class MyWidgetAction : ActionCallback {
    override suspend fun onRun(context: Context, glanceId: GlanceId, parameters: ActionParameters) {
        Intent(context, MyActivity::class.java).apply {
            // put your extras in this intent, then just start it           context.startActivity(this)
        }
    }
}
☝️ with something like that
p

Piotr Prus

03/10/2022, 4:06 PM
I tried that. You can't just start activity from widget like in the app. Originally, the widget used pending intent and glance is only a wrapper around it, so it uses same functions under.
d

daivid

03/10/2022, 5:51 PM
really? but if just adding extras is the problem, the above should work. My solution worked for a broadcast receivers... can't you use something like this then?
private fun startSomeNiceActivity(theExtra: Long, context: Context) = actionStartActivity(
    Intent(context, MyActivity::class).apply {
        putExtra("the_extra_key", theExtra)
    }
)
p

Piotr Prus

03/10/2022, 7:09 PM
I can use above code, but it will not work for multi instance widget 😞 The action for startActivity is created when content of widget is created, you cannot choose your widget params base on different glanceID. Thats the problem
m

Marcel Pinto

03/14/2022, 9:01 AM
Sorry, I am on parental leave and don’t have much time to answer questions. I don’t fully get the issue, but if you feel there is something missing on the API please open a bug or FR 🙂
p

Piotr Prus

03/14/2022, 5:23 PM
Congratulations 👏 I filled the FR
additional context: Option 1: actionStartActivity
GlanceModifier
    .fillMaxSize()
    .clickable(onClick = actionStartActivity(intent =  Intent().also { Log.d("This is called on composition, no when user click") })
Option 2: actionCallback
GlanceModifier
    .fillMaxSize()
    .clickable(onClick = actionRunCallback<MyAction>())
MyAction class:
class MyAction : ActionCallback {
    override suspend fun onRun(context: Context, glanceId: GlanceId, parameters: ActionParameters) {
        Log.d("This is called only after click")
}
m

Marcel Pinto

07/01/2022, 12:37 PM
Hey, I was looking into this again. I don’t fully get the problem. But it won’t be feasible to have an action called before starting the activity. That would create an Activity Trampoline. Right now those are allowed for Widgets but discouraged and might be blocked at some point. You could store in the state some identifier and pass that in the intent. Then in your activity you could find the GlanceID with that identifier in the state