OnCreate runs AFTER onActivityResult on Honeycomb?

2019-06-25 08:39发布

问题:

I am using startActivityForResult() to allow the user to choose a file form the file system in my app. After the file is chosen, OnActivityResult() runs and I save the file path to a variable and proceed. This works fine on Android 2.3 and lower.

On Honeycomb, however, OnCreate() runs again AFTER OnActivityReult(), essentially restarting my activity so I am unable to hold any variables because it runs an entirely new activity after I get the result.

Any idea why this is, and what I can do about it?

回答1:

One potential cause is in case the the device is low on memory, your activity was killed to claim system resources.

What can I do about it?

Save your variables in onSaveInstanceState method.

In the Activity documentation they give an exact case which might be yours, read about it (In the onSaveInstanceState method documentation).