简体   繁体   中英

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,

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

your question was answered in a different SO question here: https://stackoverflow.com/a/5060245/2898715

In my app I get the following workflow:

 onCreate onStart onRestoreInstanceState onActivityResult onResume

So, yes onActivityResult is called AFTER the onRestoreInstanceState , so you can count on the state has been fully restored (unless you do smth in onResume ).

If your Activity was not killed by OS while it was in the background, then I assume its state is the same, so you have nothing to worry about. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM