У меня была точно такая же проблема, и я обнаружил различия между разными почтовыми приложениями. Когда я выбираю Samsung Mail в окне выбора, я не сталкиваюсь с этой проблемой и могу вернуться в свое приложение, нажав назад. Однако, когда я выбираю Gmail из средства выбора, я сталкиваюсь с той же проблемой, что и вы описываете.
Я пробовал почти все возможные решения, которые мог найти в StackOverflow, но это, казалось, был единственный рабочий ответ: { ссылка } Я отправлю код на случай, если ссылка умрет.
Java:
Intent emailIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"));
PackageManager packageManager = getPackageManager();
List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(emailIntent, 0);
if (resolveInfoList.size() > 0) {
ResolveInfo resolveInfo = resolveInfoList.get(0);
// First create an intent with only the package name of the first registered email app
// and build a picked based on it
Intent chooserIntent = Intent.createChooser(packageManager.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName), "");
// Then create a list of LabeledIntent for the rest of the registered email apps
List<LabeledIntent> intentList = new ArrayList<>();
for (int i = 1; i < resolveInfoList.size(); i++) {
// Extract the label and repackage it in a LabeledIntent
resolveInfo = resolveInfoList.get(i);
String packageName = resolveInfo.activityInfo.packageName;
Intent intent = packageManager.getLaunchIntentForPackage(packageName);
intentList.add(new LabeledIntent(intent, packageName, resolveInfo.loadLabel(packageManager), resolveInfo.icon));
}
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[0]);
// Add the rest of the email apps to the picker selection
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(chooserIntent);
}
Kotlin:
val emailIntent = Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"))
val packageManager = context?.packageManager
val resolveInfoList = packageManager?.queryIntentActivities(emailIntent, 0)
if (!resolveInfoList.isNullOrEmpty()) {
var resolveInfo = resolveInfoList[0]
// First create an intent with only the package name of the first registered email app
// and build a picked based on it
val chooserIntent = Intent.createChooser(packageManager.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName), "")
// Then create a list of LabeledIntent for the rest of the registered email apps
val intentList: MutableList<LabeledIntent> = ArrayList()
for (i in 1 until resolveInfoList.size) {
// Extract the label and repackage it in a LabeledIntent
resolveInfo = resolveInfoList[i]
val packageName = resolveInfo.activityInfo.packageName
val intent = packageManager.getLaunchIntentForPackage(packageName)
intentList.add(LabeledIntent(intent, packageName, resolveInfo.loadLabel(packageManager), resolveInfo.icon))
}
// Add the rest of the email apps to the picker selection
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toTypedArray())
startActivity(chooserIntent)
}