ActivityResultLauncher callback not firing when returning from email application

I need help with detecting when a user comes back to my app after opening an email client. I’m using Jetpack Compose and trying to execute some code when the user returns from the email app.

Here’s my current setup:

val emailLauncher = rememberLauncherForActivityResult(
    contract = ActivityResultContracts.StartActivityForResult(),
    onResult = { activityResult ->
        Log.d("MyApp", "Result code: ${activityResult.resultCode}")
        // Want to run custom logic here
    }
)

And I’m launching the email app like this:

Button(
    onClick = {
        val emailIntent = Intent(Intent.ACTION_MAIN).apply {
            addCategory(Intent.CATEGORY_APP_EMAIL)
            flags = Intent.FLAG_ACTIVITY_NEW_TASK
        }
        emailLauncher.launch(emailIntent)
    }
) {
    Text("Open Email App")
}

The problem is that the callback fires immediately when I leave my app (showing result code 0), but when I navigate back from the email application, the onResult callback doesn’t get triggered again. How can I properly detect when the user returns to my app from the email client?

The issue you’re experiencing happens because email applications typically don’t cooperate well with ActivityResultLauncher. When you launch an email app with those intent flags, Android treats it as starting a separate task rather than expecting a result back. I encountered this exact problem in a project last year. What worked for me was switching to lifecycle callbacks instead of relying on activity results. Override onResume() in your activity or use Compose’s DisposableEffect with LocalLifecycleOwner to detect when your app comes back to the foreground. Another approach that sometimes works is using ACTION_SEND or ACTION_SENDTO instead of ACTION_MAIN, but this only helps if you’re actually composing an email rather than just opening the email app. The fundamental issue is that most email clients don’t send meaningful results back to the calling application.

yeah this is a pretty common issue with email apps. try removing the FLAG_ACTIVITY_NEW_TASK flag - that’s usually what causes the immediate callback. email apps dont return proper results anyway so maybe use onResume() in your activity instead to detect when user comes back?