KolaYAndr
KolaYAndr

Reputation: 7

Activity RESULT_OK never seems to happen

I'm currently working on changing the email in my app. The task is to open an email app (showing a chooser if multiple email apps are available) and, once the user selects an app, navigate to the EmailChangedSuccessfully screen.

Here is the corresponding code:

val launcher = rememberLauncherForActivityResult(
     contract = ActivityResultContracts.StartActivityForResult(),
     onResult = { result ->
         Log.d("result", result.toString())
         if (result.resultCode == Activity.RESULT_OK) onToStep(ChangeEmailStep.EmailChangedSuccessfully)
     }
)

BasicButton(
    text = stringResource(R.string.open_mail),
    modifier = Modifier.padding(top = 12.dp),
    onClick = {
        val intent = Intent
            .makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_EMAIL)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        launcher.launch(intent)
    },
    paddingValues = PaddingValues(10.dp)
)

The problem is that as soon as a chooser opens I see logged result saying this:

2025-01-31 21:31:41.469 12357-12357 result              D  ActivityResult{resultCode=RESULT_CANCELED, data=null}

I expect to get Activity.RESULT_OK when user picks an app. I've also tried this

launcher.launch(Intent.createChooser(intent, "choose email app"))

I stoped receiving RESULT_CANCELLED when chooser appears but when I come back to my app I still get RESULT_CANCELLED. What should I change to achieve the expected behaviour?

UPD: I also should mention that my activity has

android:launchMode="standard"

Upvotes: 0

Views: 30

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007474

As is covered in the documentation, the ACTION_MAIN protocol has no output, so most implementations of ACTION_MAIN will not return a result. The default is RESULT_CANCELLED.

In other words, your expectations are incorrect.

Upvotes: 0

Related Questions