Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity gets killed while executing the camera intent

[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?

like image 962
TotoroTotoro Avatar asked Mar 16 '12 19:03

TotoroTotoro


2 Answers

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.

like image 50
TotoroTotoro Avatar answered Nov 04 '22 19:11

TotoroTotoro


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.

like image 37
a.ch. Avatar answered Nov 04 '22 18:11

a.ch.