我有MainActivity,可以在其中启动相机应用程序来拍摄和上传照片。在相机应用程序中长按主页并从最近的应用程序返回后,我返回相机应用程序。如何始终从最近或单击启动器图标后返回到MainActivity?
我的相机应用程序意图:
private void showCamera() {
try {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
path = Utils.getOutputMediaFileUri(getApplicationContext());
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, path);
startActivityForResult(cameraIntent, Constant.CAMERA_REQUEST);
} catch (ActivityNotFoundException ex) {
} catch (Exception ex) {
}
}
正确的方法是将FLAG_ACTIVITY_NO_HISTORY
添加到用于启动相机的Intent
中,如下所示:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
path = Utils.getOutputMediaFileUri(getApplicationContext());
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, path);
startActivityForResult(cameraIntent, Constant.CAMERA_REQUEST);
通过添加NO_HISTORY
标志,您可以告诉Android,您不希望相机Activity
留下历史痕迹。当用户导航离开相机Activity
时(即:通过按下HOME按钮或接听来电),相机Activity
将立即完成。当用户返回到您的应用程序时(通过从最近的任务列表中选择它,或通过按下主页屏幕上的应用程序图标),相机Activity
将不再位于顶部。
在您的主要活动中
android:launchMode="singleTask"
在Manifest.xml文件夹
p>在您的主要活动的清单上android:excludeFromRecents="true"
android:launchMode="singleTask"