1

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

1 Answers1

0

This SO Link has details on when the onRestoreInstanceState method is called

For quick reference, i am pasting this from the other link

onRestoreInstanceState (or saved bundle in onCreate) will be fired when the Activity was killed by the system due to lack of resources and restarted when you get back to it. So t if you start a new Activity in front and getting back to the previous (what you are trying to do), the Activity A might not be killed (just stopped) and restarted without going through onRestoreInstanceState.

Community
  • 1
  • 1
Shiv
  • 689
  • 8
  • 23