# SizeMode

Artifact: [androidx.glance:glance-appwidget](https://developer.android.com/jetpack/androidx/releases/glance) [View Source](https://cs.android.com/search?q=file:androidx/glance/appwidget/SizeMode.kt+class:androidx.glance.appwidget.SizeMode) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/glance#1.0.0)


```
sealed interface SizeMode
```

<br />

Known direct subclasses [PreviewSizeMode](https://developer.android.com/reference/kotlin/androidx/glance/appwidget/PreviewSizeMode), [SizeMode.Exact](https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Exact), [SizeMode.Responsive](https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Responsive), [SizeMode.Single](https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Single)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/PreviewSizeMode` | This marker interface determines which `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode`s can be used for preview compositions. |
| `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Exact` | The `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` provides a UI for each size the App Widget may be displayed at. |
| `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Responsive` | The `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` provides a UI for a fixed set of sizes. |
| `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Single` | The `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` provides a single UI. |

Known indirect subclasses [SizeMode.Responsive](https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Responsive), [SizeMode.Single](https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Single)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Responsive` | The `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` provides a UI for a fixed set of sizes. |
| `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Single` | The `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` provides a single UI. |

*** ** * ** ***

Modes describing how the `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` should handle size specification.

Note: Size modes that support multiple sizes (`https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Exact`, `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Responsive`) run the composable passed to to `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/package-summary#(androidx.glance.appwidget.GlanceAppWidget).provideContent(kotlin.Function0)` concurrently for each size. This has a number of important implications. Since an instance of the content is running for each size, all of the State objects in the content will have an instance for each size.

For example, in Exact mode, let's say the AppWidgetHost asks for 2 sizes, portrait and landscape. In the code below, there will end up being two instances of the `count` variable, one for each size:

```kotlin
provideContent {
   var count by remember { mutableStateOf(0) }
   Button(
       text = "Increment count: $count",
       onClick = { count++ }
   )
}
```

If the button is clicked while the widget is displayed in portrait size, the `count` variable is updated for both sizes. This is so that, if the device orientation changes and the host displays the landscape layout, it will be consistent with the state in portrait. This works because lambdas that are at the same place in the composition will be mapped to the same default key in all sizes. So triggering one will trigger the corresponding lambdas in other sizes.

This means that lambdas will be called multiple times when they are triggered, which can have unexpected effects if state is not handled correctly. In order to prevent some external action from being triggered multiple times at once, you should conflate requests so that only one is active at a time, e.g. using MutableStateFlow.

To prevent this behavior, you can use the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/key.composable#key(kotlin.Array,kotlin.Function0)` composable to set different default lambda keys for each size, so that they do not trigger each other:

```kotlin
provideContent {
   key(LocalSize.current) {
       var count by remember { mutableStateOf(0) }
       Button(
           text = "Increment count: $count",
           onClick = { count++ }
       )
   }
}
```

To disable this behavior on a per-lambda basis, use `https://developer.android.com/reference/kotlin/androidx/glance/action/action.composable#action(kotlin.String,kotlin.Function0)` to set a custom lambda key based on the current size:

```kotlin
provideContent {
   var count by remember { mutableStateOf(0) }
   Button(
       text = "Increment count: $count",
       onClick = action("incrementCount-${LocalSize.current}") { count++ }
   )
}
```

In both of the last two examples, when the button is clicked, only the lambda for the currently visible size will be triggered.

Note that the above does not work for effects, which will always be triggered for each size. Use effects to update state variables in the composition, otherwise be sure to handle any duplicate triggering that may occur.

## Summary

| ### Nested types |
|---|
| `object https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Exact : https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode` The `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` provides a UI for each size the App Widget may be displayed at. |
| `class https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Responsive : https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode, https://developer.android.com/reference/kotlin/androidx/glance/appwidget/PreviewSizeMode` The `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` provides a UI for a fixed set of sizes. |
| `object https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode.Single : https://developer.android.com/reference/kotlin/androidx/glance/appwidget/SizeMode, https://developer.android.com/reference/kotlin/androidx/glance/appwidget/PreviewSizeMode` The `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceAppWidget` provides a single UI. |