Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera or Gallery intent destroys old activity on some devices

I am working on app which uses WebView to display its content. However, it needs to open camera or gallery in order to choose picture:

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, 1);

    Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent, 2);

It's working fine on most devices, but on HTC One and few others both intents destroys my activity, so webview is being reloaded while going back. I don't have noHistory flag in AndroidManifest.xml. What might be causing that issue? Can I avoid destroying my activity here?

like image 645
Piotr Avatar asked Jan 31 '13 09:01

Piotr


2 Answers

It is normally, that Android kills your Activity when other app runs.

You must save Activity state in onSaveInstanceState and when activity will be recreated restore state in onRestoreInstanceState or in onCreate.

To restore state of WebView you may use cookies and sessions and save last opened url. When activity will be recreated just navigate WebView last saved url and process result from camera.

like image 160
Nik Avatar answered Oct 23 '22 09:10

Nik


Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1);

By seeing your code I can judge that your motto is to capture the image and use it later on.

This is a known bug, The solution is that you need to create a separate folder for your application and before capturing you need to be sure that file is created and the same path you are giving to camera intent

Uri uriSavedImage=Uri.fromFile(new File("/sdcard/seperate/newImage.png"));
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("output", uriSavedImage);
startActivityForResult(cameraIntent, 1);

Reference: image from camera intent issue in android

like image 34
Mohammed Azharuddin Shaikh Avatar answered Oct 23 '22 09:10

Mohammed Azharuddin Shaikh