14

I'm sending an intent to the camera from an activity that I call like this:

 Intent testphoto = new Intent(Dashboard.this,CameraHandler.class);
 startActivity(testphoto);

In the CameraHandler class I call the camera:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, 0);

But before onActivityResult gets called in the CameraHandler class the activity is destroyed. Is there anyway to prevent this?

FOUND THE ANSWER: I had noHistory="true" in my AndroidManifest and that made the OS destroy the activity before the result.

Diego
  • 4,011
  • 10
  • 50
  • 76
  • how do you know its getting destroyed? – dymmeh May 02 '12 at 17:32
  • I´ve put a Log in the OnDestroy() method, I tried a single app with only one activity and it works fine, but when I start using this in a more complex app the activity gets destroyed before the camera returns the image – Diego May 02 '12 at 17:44
  • If you've found your own answer, post it and accept your own answer in order to close the question. Thank you. – davidcesarino May 02 '12 at 19:25

3 Answers3

43

Be sure you don't have the "Don't keep activities" Developer setting on, as it will destroy the activity you are leaving.

Omaraf
  • 833
  • 8
  • 15
4

You don't have to worry about the calling Activity being destroyed when you call startActivityForResult(), as it won't change the expected behavior at all (i.e. the child activity will remember to pass the result back to the parent whether the parent is destroyed or not). See this post.

Also note that while it is sometimes necessary to prevent Activitys from being destroyed (i.e. on configuration changes, etc.), in general you want to leave the Activity lifecycle alone and let the system manage background Activitys for you. Once you launch a new Activity, you shouldn't be explicitly preventing previous Activitys from being destroyed.

Community
  • 1
  • 1
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • 2
    Only thing is that suddenly onActivityResult never is called, when I have an App with only one activity, the activity is never destroyed, but onActivityResult is called, now when inside another app suddenly the activity is destroyed and the result never seem to come back. Any idea how that can be? – Diego May 02 '12 at 17:58
  • Perhaps there is something wrong with your workflow. It seems odd that you are calling `startActivityForResult()` twice. You definitely need to call `startActivityForResult()` when launching the actual camera application, but I'm not so sure that you need it for the `CameraHandler` class. What is the `CameraHandler` class anyway? Is it possible to launch the camera app directly from the dashboard? – Alex Lockwood May 02 '12 at 18:03
  • I need a screen in between, so directly from the dashboard is not an option, I found that startActivityForResult would prevent destroying the activity, but it does not, so indeed I might just as well remove that. The Camerahandler sends the intent to the camera and uploads a photo onactivity result, which it doesnt do now.. – Diego May 02 '12 at 18:24
  • OK, so just to be clear... you have removed the `startActivityForResult()` call from your dashboard? (you might want to update your original post). – Alex Lockwood May 02 '12 at 18:38
  • Have you taken a look at this? I can't tell you for sure what is wrong with your code based on the information you have given me, but I can tell you that something is definitely wrong if `onActivityResult` is never called when the `Activity` returns. Check out the sample code [**here**](http://developer.android.com/training/camera/index.html) and see if it helps. – Alex Lockwood May 02 '12 at 18:41
  • thanks, hopefully I´ll find something. I posted the entire camerahandler class. – Diego May 02 '12 at 18:46
  • @AlexLockwood hello can you please tell me the right way to start activity startActivityForResult i m facing same issue just because of activity being killed – Ando Masahashi Nov 21 '14 at 12:17
  • "it won't change the expected behavior"- sure it can. if you keep state in your activity and don't save that state. – Jeffrey Blattman Oct 09 '15 at 21:59
-3

You have to mention in your manifest as

 android:persistent="true"

Below SO posts answers the same question

How to make an activity stop, rather then be destroyed, from the BACK key?

How to prevent call of onDestroy() after onPause()?

Prevent activity from being destroyed as long as possible

Community
  • 1
  • 1
Aravind.HU
  • 9,194
  • 5
  • 38
  • 50