9

[EDIT: Problem solved. Please see my answer below.]

In my app I call the system camera to take a picture, and then handle the result in onActivityResult. You know, the usual. It used to work, but now my calling activity gets killed while I'm taking the picture. Specifically, onDestroy() is called on my activity right after I press the camera shutter. The photo does get taken & saved (I've checked that the file gets written on the SD card). After I accept the photo, instead of returning to the calling activity and invoking onActivityResult, the previous activity in the activity stack gets called. I see no exceptions in the logcat. My custom exception handler doesn't get called. If it matters, my app also includes a service that listens to GPS updates, but I unregister all the receivers in onPause().

Here's the call stack for MyCallingActivity.onDestroy():

Thread [<1> main] (Suspended (breakpoint at line 303 in NewPlaceDetailsActivity))   
    NewPlaceDetailsActivity.onDestroy() line: 303   
    ActivityThread.performDestroyActivity(IBinder, boolean, int, boolean) line: 2663    
    ActivityThread.handleDestroyActivity(IBinder, boolean, int, boolean) line: 2694 
    ActivityThread.access$2100(ActivityThread, IBinder, boolean, int, boolean) line: 117    
    BinderProxy(ActivityThread$H).handleMessage(Message) line: 968  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 130 
    ActivityThread.main(String[]) line: 3687    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 507  
    ZygoteInit$MethodAndArgsCaller.run() line: 842  
    ZygoteInit.main(String[]) line: 600 
    NativeStart.main(String[]) line: not available [native method]  

This is how I start the camera activity, in case you're wondering:

protected void startCamera() {
    createPhotoDirsIfNeeded();

    String fileName = "temp.jpg";  
    ContentValues values = new ContentValues();  
    values.put(MediaStore.Images.Media.TITLE, fileName);  
    m_capturedImageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

    m_photoFileName = APP_PHOTO_PATH + "/" + DateFormat.format(DATE_FORMAT, Calendar.getInstance().getTime()) + ".jpg";
    File picFile = new File(m_photoFileName);
    if(picFile.exists()) {
        picFile.delete();
    }

    // start the camera activity
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,  Uri.fromFile(picFile));
    startActivityForResult(intent, IntentHelper.REQUEST_TAKE_PHOTO);
}

How can I find out why does my activity get killed, AND removed from the stack instead of being created again?

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76
  • Post related code. It seems your code killing app may be calling some commands related to kill activity. – kosa Mar 16 '12 at 20:21
  • Posted some more code. onActivityResult() never gets called because the activity is destroyed by the time I return from the camera. – TotoroTotoro Mar 16 '12 at 20:26
  • In `NewPlaceDetailsActivity.onDestroy()`, check whether `isFinishing()` returns true. If not, your activity is being destroyed in order to be restarted (perhaps due to an orientation change while the picture was being taken). – Ted Hopp Mar 16 '12 at 20:35
  • isFinishing() returns true. I also made sure not to change the orientation this time. Also, even if it were restarting due to orientation change, onActivityResult would still be called, no? – TotoroTotoro Mar 16 '12 at 20:44
  • 2
    First, it is eminently possible that your process will be terminated, to free up memory. There is nothing wrong with that. With regards to your exception, you are trapping the exception in Eclipse. Let execution proceed, then examine the LogCat stack trace. If it still says you have a problem on line 303 in your `NewPlaceDetailsActivity`, you will need to examine what is on that line. – CommonsWare Mar 16 '12 at 21:20
  • @CommonsWare: I don't get any exception. I just set a breakpoint to confirm that my activity is indeed being destroyed. When I remove the breakpoint, the activity does NOT get re-created (which I would expect), and the activity previous to it in the stack gets opened. – TotoroTotoro Mar 17 '12 at 04:11
  • possible duplicate of [Android: Activity getting Destroyed after calling Camera Intent](http://stackoverflow.com/questions/16014930/android-activity-getting-destroyed-after-calling-camera-intent) – Alex Cohn Sep 29 '15 at 20:25

3 Answers3

9

OK, I found the problem. The issue was not so much that the activity was being killed, but that it was not being restored. I realized that this can happen when your activity is declared in the manifest with android:noHistory="true". So the activity was removed from the stack upon being killed, and the previous activity was shown instead.

After I removed the noHistory parameter, the activity started appearing again after using the camera.

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76
  • i think its orintation change problem.you can solve this problem from your manifest file too. – Akram Mar 25 '12 at 03:38
  • No, it's not the orientation change. I made sure the orientation stayed the same during my testing. – TotoroTotoro Mar 25 '12 at 03:40
  • oh yeah android:noHistory="true" if you keep this means you are not monitering the state of the activity simply means when control goes away from your activity its being killed by the system. – Akram Mar 25 '12 at 03:46
2

I guess the problem is 1) in your custom implementation of exception handler and 2) this line:

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(picFile));
  1. Your exception handler implementation doesn't allow you to see the NullPointerException which is thrown in onActivityResult method when you try to access the data from the Intent returned.
  2. It seems to be a bug which prevents Intent from being passed to onActivityResult method if an output file has been put as an extra to the Intent starting camera Activity.

Try removing the line and see if your Activity is recreated when camera Activity has been finished.

a.ch.
  • 8,285
  • 5
  • 40
  • 53
  • 1
    I did have the same issue with the EXTRA_OUTPUT. It seems android doesnt like you to specify an output folder. This is more critical for video where there is buffering and streaming is involved. I solved the problem by waiting the photo or video to be taken then moving it where the user want it to be. – Gomoku7 Mar 19 '12 at 13:41
  • Hi -- I've removed the line where I specify the file name. I also removed the custom exception handler (which was only turned on to debug this issue in the first place). The problem is still there though. So there must be something else. – TotoroTotoro Mar 19 '12 at 18:47
  • Don't you get any exception? Show your `onActivityResult` code. – a.ch. Mar 19 '12 at 20:32
  • @a.ch.: onActivityResult was never called. I have found out why, please see my own answer. – TotoroTotoro Mar 22 '12 at 18:26
0

Please follow this steps:

1)  Add this in manifest :
    <uses-feature android:name="android.hardware.camera"></uses-feature>

2)  startCamera() :
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,  Uri.fromFile(picFile));
    startActivityForResult(intent, IntentHelper.REQUEST_TAKE_PHOTO);


3)  if (requestCode == PICTURE_RESULT) //
             if (resultCode == Activity.RESULT_OK) {
                // Display image received on the view
                 Bundle b = data.getExtras(); // Kept as a Bundle to check for other  things in my actual code
                 Bitmap pic = (Bitmap) b.get("data");

                 if (pic != null) { // Display your image in an ImageView in your layout (if you want to test it)
                     pictureHolder = (ImageView) this.findViewById(R.id.IMAGE);
                     pictureHolder.setImageBitmap(pic);
                     pictureHolder.invalidate();
                 }
             }
             else if (resultCode == Activity.RESULT_CANCELED) {...}
    }

If still this code doesn't work for you, please refer to following links :

Community
  • 1
  • 1
Mohanish
  • 469
  • 2
  • 13