onNewIntent in jetpack compose

Viewed 455

I am recieving some text from other apps, for example if people share a video from youtube app, my app will show up in the shareable list. If they choose my app, I want to get the text content.

I have an intent-filter like this.

<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:host="*" android:mimeType="text/*" />
</intent-filter>

I can access the data using the onNewIntent method from the activity.

override fun onNewIntent(intent: Intent?) {
    Log.d(TAG, intent?.getStringExtra(Intent.EXTRA_TEXT).toString())
    super.onNewIntent(intent)
}

But I want to access the text inside a composable function, how can I do that?

1 Answers

I think using a disposable effect in the setContent method should do the work

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            DisposableEffect(Unit) {
                val listener = Consumer<Intent> {
                    // logic here
                }
                addOnNewIntentListener(listener)
                onDispose { removeOnNewIntentListener(listener) }
            }
        }
    }
Related