0

When I take a picture with the camera in my app the app/activity restarts (and all other activities are closed too) when I save the picture I took. Also when I debug the app, the debugger disconnects after taking the picture.

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

File file = getOutputMediaFile(1);
picUri = Uri.fromFile(file);
i.putExtra(MediaStore.EXTRA_OUTPUT, picUri);

startActivityForResult(i, 0);

My onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
            case 0:
                mProgress.show();
                loadCameraImage();
                break;

            case 1:
                mProgress.show();
                Uri uri = imageReturnedIntent.getData();
                loadFileImage(uri);
                break;
        }
    }
}
Bart Bergmans
  • 4,061
  • 3
  • 28
  • 56

1 Answers1

2

Android had to free up memory for the camera app, and your process was terminated. It will be automatically restarted when the camera app exits and control returns to your app. This is perfectly normal, and your app's process will be terminated in many other cases when your app is not in the foreground.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • How can I make sure this doesn't happen. When I worked on it yesterday the app didn't restart.. – Bart Bergmans May 28 '15 at 17:25
  • @BartBergmans: "How can I make sure this doesn't happen" -- you can't. Again, this is perfectly normal behavior. If your app is not handling this case, you need to fix your app. "When I worked on it yesterday the app didn't restart" -- which process(es) Android will terminate to free up system RAM depends on what processes are around and their various states. In this case, the camera app you are using probably is asking for a large heap, forcing Android to terminate other processes more aggressively to handle the camera app's needs. – CommonsWare May 28 '15 at 17:30
  • But I am using the code from http://stackoverflow.com/questions/16026818/actionbar-custom-view-with-centered-imageview-action-items-on-sides (top answer) to draw my ActionBar so if the app restarts the actionbar looks ugly until the actionbar is drawn again. – Bart Bergmans May 28 '15 at 17:34
  • @BartBergmans: And the action bar will look ugly in other scenarios as well, not just this one. For example, the user is in your app, presses HOME, Android terminates your process, and the user returns to your app (say, 20 minutes later). Your process will be recreated, the user will be taken to the activity she was last on, and you will have the same ugly result. This is not unique to taking photos. Either live with the ugly, or fix your action bar rewrite to be less ugly in this case. – CommonsWare May 28 '15 at 17:38
  • When I open the app again from HOME the actionbar looks good. How can I rewrite that function that it will show the good actionbar every time? – Bart Bergmans May 28 '15 at 17:44
  • @BartBergmans: "When I open the app again from HOME the actionbar looks good" -- your process may not have been terminated between pressing HOME and returning to the app. If you wish to test this, press HOME, terminate the process using DDMS, then return to the app (e.g., recent-tasks list). "How can I rewrite that function that it will show the good actionbar every time?" -- I have no idea, in part because I don't know what "ugly" means in this context. Perhaps you should ask a separate SO question on how to address this. – CommonsWare May 28 '15 at 17:48
  • Hmm, I'll leave it as it is then. Thanks for the time and the knowledge! – Bart Bergmans May 28 '15 at 18:02
  • @CommonsWare i have a question please, supposed i handled the activity restart issue in onSaveInstanceState and in onCreate i restored the data will i still get the image? Will onActivityResult still be called? – Mohammad Haidar Sep 01 '16 at 14:07
  • @Haidar: Yes, `onActivityResult()` should be called. It will just be called on a new instance of the activity, in a new process, if your process had been terminated while the camera app was in the foreground. – CommonsWare Sep 01 '16 at 14:12