Search code examples
androidandroid-activitywidgetglance-appwidgetglance

Glance widget actionStartActivity not working properly


I am trying to implement a glance widget and start an activity while clicking on a button. But it seems like it's not working properly for me. This is my code:

 DistributionDateView(context, deliveryDates, postalCode) {
                println("it should work i think")
                actionStartActivity(AddParcelManuallyActivity.newIntent(context = context))
                actionStartActivity<AddParcelManuallyActivity>()
                actionStartActivity(AddParcelManuallyActivity::class.java)
}

Where DistributionDateView has a onclick. I have tried 3 different ways, "it should work I think" is printing in the logs... but nothing is happening. What am I doing wrong here.


Solution

  • actionStartActivity is just return a object of interface StartActivityAction. It should be used in some glance function. It is not an alternative of context.startActivity which is a function.

    //source code in package androidx.glance
    fun GlanceModifier.clickable(onClick: Action): GlanceModifier =
        this.then(ActionModifier(onClick))
    
    //source code in package androidx.glance
    @Composable
    fun Button(
        text: String,
        onClick: Action,
        modifier: GlanceModifier = GlanceModifier,
        enabled: Boolean = true,
        style: TextStyle? = null,
        colors: ButtonColors = ButtonDefaults.buttonColors(),
        maxLines: Int = Int.MAX_VALUE,
    )
    

    and one example is

    @Composable
    fun MyContent() {
        Image(
            provider = provider,
            modifier = GlanceModifier
                .clickable(actionStartActivity<MainActivity>()),
            contentDescription = ""
        )
    }