I was following this article to make a compose app a share target, but it wasn’t working.
After adding an intent filter to the manifest, the app can be opened via the share menu.
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="_">
<!-- android:launchMode="singleInstance"-->
<!-- android:allowTaskReparenting="true"-->
<!-- >-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
(not sure about launch mode)
However, it seems that the activity isn’t re-created, so I am not able to print the intent with its data.
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d("IMPORT 2", "$intent")
setContent {
}
}
}
Also, it isn’t the correct composable that is shown. It only works if I close the app first so that the send intent opens the app for the first time.
@Composable
fun Navigation(appState: PoiAppState, paddingValues: PaddingValues) {
NavHost(appState.navController, startDestination = Screen.Home.route, Modifier.padding(paddingValues)) {
...
composable(
route = "share_target_route"
deepLinks = listOf(
navDeepLink {
action = Intent.ACTION_SEND
mimeType = "text/*"
},
navDeepLink {
action = Intent.ACTION_SEND
mimeType = "image/*"
}
)
) {
ShareTargetScreen()
}
...
}
}