Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult() function when Activity is recreated?

I have a problem : When I take a photo in my apps,my app open Camera app and take a photo but when return Activity seem my device is low memory and onCreate() function is recalled. In this case onActivityResult() is called before onCreate() or onCreate() is called before onActivityResult() ? Is there a sequence diagram for Activity's @Override function?

    public void dispatchTakePictureIntent(int actionCode) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File dir= new File("/sdcard/Test/");
    dir.mkdir();
    String fileName = "IMG_"+System.currentTimeMillis()+".JPG";
    GlobalData.IMAGE_PATH_CAMERA = "/sdcard/Test/"+fileName;
    File output= new File(dir, fileName);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
    startActivityForResult(takePictureIntent, 1);
}

Thank in advanced,

like image 329
nguoitotkhomaisao Avatar asked Mar 25 '26 06:03

nguoitotkhomaisao


1 Answers

The reason the onCreate() is called again isn't always because your device is low in memory, one of the most common reasons is the change in orientation which causes the activity to recreate so you can try putting this in your manifest file:

android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize"

however, if you experiment enough with your code and come to conclusion that it is caused by android destroying the activity because of memory shortage then here is a work around you can use to correctly restore your activity.

YourObject mObject;
    .
    .
    .

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        mObject.setImageUri(bla..); //Store image Uri and other values in mObject
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);

        if (mObject != null) {
            //save your object which contains data like image uri
            savedInstanceState.putSerializable(KEY, mObject);
        }
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        YourObject obj = (YourObject) savedInstanceState.getSerializable(KEY);

        if(obj != null)
            mObject = obj;
    }

and finally, in the onResume, read your stored data back from mObject if its not null

    @Override
    protected void onResume() {
        super.onResume();

        if(mObject != null) {
            //set image to imageView using the stored Uri
            setImage(imageView, mObject.getImageUri());
        }
    }

Sequence:

    OnResume
    OnPause
    OnSaveInstanceState - Here we save our data in bundle
    OnDestroy - Here we lose all our activity data
    OnCreate
    onRestoreInstanceState - Here we restore our saved data from bundle if any
    OnResume - Here we deal with restored data if any
like image 190
Obaidah Avatar answered Mar 27 '26 18:03

Obaidah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!