Is there a method of the Activity lifecycle which is called if the user presses the back button, but not if the the method start开发者_开发技巧ActivityForResult() is called? I couldn't find a method by testing it.
I'm sorry, I just didn't saw the solution, but here it is:
By calling startActivityForResult(), the methods onPause() and onStop() of the original activity are called.
When pressing the Back Button, the methods onPause(), onStop() and onDestroy() are called.
So the difference is the onDestroy() method.
Normally, it will be onResume() followed by onActivityResult(). However it's possible, though unlikely, that the calling activity will have been killed at some point while the user worked with the other activity; this happens when the system runs out of memory, at which point it starts killing stuff, starting from the 'most inactive'. In that case, I imagine it would go through onCreate(), onStart(), onResume() and then finally onActivityResult(). https://stackoverflow.com/a/2869832/323696
The answer I quoted above is correct, except for the explanation for when the calling activity is killed, or finished(), before the called activity completes.
In that case, when the calling activity, Activity #1, resumes after having called another activity, Activity #2, using startActivityForResult, the method onActivityResult in Activity #1 is called BEFORE onResume.
This is important to know if you are instantiating your SQLite Database objects from within onResume in Activity #1. If so, you will also need to instantiate the object from within onActivityResult, when returning from Activity #2.
For more information, read about the 'startActivityForResult method' at http://developer.android.com/reference/android/app/Activity.html.
I haven't been able to find a LifeCycle Diagram depicting this. The step for when returning from an activity for result is always summed up within a text description in the LifeCycle images, stating 'User Returns to the activity', or 'The activity comes to the foreground.'
精彩评论