1

I have MainActivity in which I can launch camera application to take and upload photo. After home long press while in camera app and returning back from recents I return to camera application. How can I ALWAYS return to MainActivity from recents or after launcher icon click?

My camera app intent:

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) {
    }

}
David Wasser
  • 93,459
  • 16
  • 209
  • 274

3 Answers3

0

In your main activity

android:launchMode="singleTask"

in the Manifest.xml folder

Bryan Mills
  • 115
  • 9
0

At manifest for your main activity

android:excludeFromRecents="true" 
android:launchMode="singleTask"
ViramP
  • 1,659
  • 11
  • 11
  • Thanks, this is kind of solution, but not exactly what i want. This excludes my whole application from recents. I dont want that, I just want to return to MainActivity from icon and from recents. – Vlad Hanych Aug 04 '16 at 08:19
0

The correct way to do this is to add FLAG_ACTIVITY_NO_HISTORY to the Intent you use to launch the camera, like this:

    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);

By adding the NO_HISTORY flag, you tell Android that you don't want the camera Activity to leave a historical trace. When the user navigates away from the camera Activity (ie: by pressing the HOME button, or taking an incoming phone call), the camera Activity will be immediately finished. When the user then returns to your app (either by selecting it from the list of recent tasks, or by pressing the app's icon on the HOME screen) the camera Activity will no longer be on top.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thanks for answer! The best solution at the moment, everything works as i wanted, but there is a problem: in the most common case, when i launch cameraIntent from MainActivity -> take picture -> save it -> in onActivityResult() method I receive RESULT_CANCELED for some reason. Furthermore i ALWAYS receive RESULT_CANCELED with this intent flag. Why? Does camera activity finish before returning result? – Vlad Hanych Aug 05 '16 at 12:44
  • That could be a problem. This situation is very complicated. Why do you want this behaviour? The standard behaviour is to return to the camera `Activity` if the user left your app with the camera on top. – David Wasser Aug 05 '16 at 13:10
  • I've tried this code using a simpler mechanism, where I just launch the camera `Activity` to take a photo and return it in `onActivityResult()` (without specifying `EXTRA_OUTPUT`). This works and I get RESULT_OK and the photo back. If the camera `Activity` is launching some other `Activity` to save the photo, this could cause the problem you are seeing. – David Wasser Aug 05 '16 at 13:19
  • I need this behaviour to avoid my main problem. The main problem is that if user leaves my task while in camera Activity and after this system kills task, after returning to my app task and taking photo MainActivity dont always restart and sometimes user see home screen) – Vlad Hanych Aug 05 '16 at 14:30
  • Ah...then you should try to fix your main problem and not spend time on workarounds like this. It will be difficult to get this working exactly the way you want, because you need to use `startActivityForResult()` to launch the camera `Activity`. This means that the camera `Activity` must run within your task (it cannot run in a separate task). – David Wasser Aug 05 '16 at 15:17