Reputation: 33
I'm trying to implement functionality similar to Google Keep's sharing behavior. When sharing content (like text) from another app and selecting Google Keep from the share menu, the app doesn't open its MainActivity or the full app UI. Instead, it shows a simple overlay (an Activity) to handle the shared content, while bypassing the full app launch.
In my app, I want to achieve the same behavior:
When the user selects my app from the share menu, only an overlay Activity should appear. The MainActivity (the default entry point when opening the app normally) should not be launched at all when sharing content from another app. This overlay should only handle the shared Intent and appear on top of the current app without fully launching my app's UI. I've tried using intent filters and launch modes, but the MainActivity still gets launched before the overlay Activity appears, or the app behaves unexpectedly.
Could someone guide me on how to implement this correctly? How can I prevent the MainActivity from being triggered entirely when handling shared intents?
Thank you!
Upvotes: 1
Views: 55
Reputation: 1833
The Keeps app is launching an activity as a result of an intent action. It is a transparent activity without state or history. The activity is registered in the Android manifest with a mimeType
- you can choose a MIME type you are interested in and perform some work after parsing out the data.
Here's how you can achieve a similar effect:
First, declare an Activity which can receive a suitable intent in your AndroidManifest
:
<activity
android:name=".ShareActivity"
android:exported="true"
android:launchMode="singleInstance"
android:noHistory="true"
android:stateNotNeeded="true"
android:theme="@style/Theme.ShareTest.Transparent">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
I've set some attributes to influence how the system handles the activity on the recents screen - modify as needed.
Note that my theme is setup to be transparent in nature in themes.xml
:
<style name="Theme.ShareTest.Transparent" parent="Theme.Material3.DayNight.NoActionBar">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Here's the ShareActivity
I setup:
class ShareActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intent = this.intent
when {
intent?.action == Intent.ACTION_SEND -> {
if ("text/plain" == intent.type) {
// pull out the text from the intent
val text: String? = intent.getStringExtra(Intent.EXTRA_TEXT)
// do something useful with the result
MaterialAlertDialogBuilder(this).apply {
setMessage("Received some text: $text")
setPositiveButton("Okay") { dialog, _ -> dialog.dismiss() }
}.show()
}
}
else -> {
// handle other intent actions...
}
}
}
}
You can perform whatever work you need, and also supply any UI you want. I'm using a MaterialAlertDialogBuilder
above to show a simple alert dialog with the incoming intent action String extra in the message.
Upvotes: 1