I am writing an android application where I accept data in a form and then call the android camera for uploading an image via the below code:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
The call works just fine and the camera image is successfully returned. But the problem comes after this.
I have been testing on 2-3 phones, namely: 1. Micromax a116 2. Samsung Galaxy Grand 3. Xiaomi
The application runs just fine on the Micromax phone.
There was an issue with the Samsung phone which gave app crashes on rotation. I found a fix for that by adding below to Manifest:
android:configChanges="orientation|keyboardHidden|screenSize"
But there's an issue with the Xiaomi phone. It always seems to call the "onCreate()" method, everytime the camera intent returns an image. This disturbs the previous form data that I have accepting from the user, by making unwarranted API calls.
I tried overriding the onSaveInstanceState and onRestoreInstanceState by the following code:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on scren orientation
// changes
outState.putParcelable("file_uri", fileUri);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file url
fileUri = savedInstanceState.getParcelable("file_uri");
}
but onRestoreInstanceState is never called. Thanks