Reputation: 11
I have a problem creating a Tile for Wear OS with GlanceTileService. I put there a button that should take the user to the MainActivity of the app (which works), but I cannot seem to figure out how to pass a parameter with it. I read that in JetPack compose you should have only one Activity, so the general idea is there would be two or more buttons and the params in the intent would help to differate what the user wants. I am a beginner, so please bear with me, if I missed something obvious. :)
This is my implementation:
implementation("androidx.glance:glance-wear-tiles:1.0.0-alpha05")
And this is the Manifest part related to the Tile and Main Activity:
<service android:name="com.example.test.tile.FirstTile"
android:exported="true"
android:label="First Tile"
android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER">
<intent-filter>
<action android:name="androidx.wear.tiles.action.BIND_TILE_PROVIDER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="androidx.wear.tiles.PREVIEW"
android:resource="@drawable/tile_preview" />
</service>
<uses-library
android:name="com.google.android.wearable"
android:required="true" />
<meta-data
android:name="com.google.android.wearable.standalone"
android:value="true" />
<activity
android:name=".presentation.MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Now this is my Tile:
@ExperimentalComposeUiApi
class FirstTile : GlanceTileService() {
@Composable
override fun Content() {
Column(
modifier = GlanceModifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalAlignment = Alignment.Top
) {
Row(
modifier = GlanceModifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(
modifier = GlanceModifier.size(58.dp), text = "Try me",
onClick = actionStartActivity<MainActivity>
(
actionParametersOf(
ActionParameters.Key<String>("hello") to "android"
)
)
)
}
}
}
}
And this is the starting code in MainActivity to catch the param/intent:
@ExperimentalComposeUiApi
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d("Check whole intent -->", intent.toUri(0))
Log.d("Check hello -->", intent.extras?.getString("hello").toString())
But the results from log are:
Check whole intent --> D #Intent;launchFlags0x10400000;component=com.example.test/.presentation.MainActivity;sourceBounds=134%20134%20250%20250;end
Check hello --> D null
So it seems that the actionparams are not passed to the intent. As I understand using the standard intent is not possible in the Glance Tile since the onClick/.clickable expects Action, so I was trying to use this function:
Creates an Action that launches the specified Activity when triggered.
Params:
activity - class of the activity to launch
parameters - the parameters associated with the action. Parameter values will be added to the activity intent, keyed by the parameter key name string.
fun <T : Activity> actionStartActivity(
activity: Class<T>,
parameters: ActionParameters = actionParametersOf()
): Action = StartActivityClassAction(activity, parameters)
Upvotes: 1
Views: 142