android - Activity RESULT_OK never seems to happen - Stack Overflow

admin2025-04-17  2

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"

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"
Share Improve this question edited Jan 31 at 19:15 KolaYAndr asked Jan 31 at 19:13 KolaYAndrKolaYAndr 73 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://anycun.com/QandA/1744850209a88494.html