I have a problem with passing parameters in action...
# glance
p
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
yeah, it should work
Copy code
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
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
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?
Copy code
private fun startSomeNiceActivity(theExtra: Long, context: Context) = actionStartActivity(
    Intent(context, MyActivity::class).apply {
        putExtra("the_extra_key", theExtra)
    }
)
p
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
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
Congratulations 👏 I filled the FR
additional context: Option 1: actionStartActivity
Copy code
GlanceModifier
    .fillMaxSize()
    .clickable(onClick = actionStartActivity(intent =  Intent().also { Log.d("This is called on composition, no when user click") })
Option 2: actionCallback
Copy code
GlanceModifier
    .fillMaxSize()
    .clickable(onClick = actionRunCallback<MyAction>())
MyAction class:
Copy code
class MyAction : ActionCallback {
    override suspend fun onRun(context: Context, glanceId: GlanceId, parameters: ActionParameters) {
        Log.d("This is called only after click")
}
m
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
107 Views