OnActivityResult () & onResume ()

Can someone tell me what is called first, is it onActivityResult() or is it onResume() ? Example:

Action A calls startActivityForResult() to start Activity BB executes, terminates, and returns the result of A, but which method A is called first, onActivityResult() or onResume() ?

I know that someone already answered this question by contacting Activity Docs , but I could not find it myself.

+80
android
Jun 24 '11 at 13:15
source share
4 answers

The first call to onActivityResult() , then onResume() .

Quote from the docs:

protected void onActivityResult (int requestCode, int resultCode, Intent data)

Since: API Level 1 It is called when the activity that you started up issues you request the code that you started it with, resultCode it returned, and any additional data from it. resultCode will be RESULT_CANCELED if the action explicitly returned it, did not return any result, or did not crash during its operation. You will receive this call immediately before onResume (), when your activity re-starts.

+103
Jun 24 2018-11-11T00:
source share

As others have already noted, onActivityResult () is called before onResume () when your activity restarts.

Diana Hackborn explains that onActivityResult () is called before onResume () to allow anything that can affect the user interface that must be received and accessible before updating the user interface (presumably to avoid double updating - once in onResume () without return result, then in onActivityResult (), adding the return result).

https://groups.google.com/forum/?fromgroups=#!topic/android-developers/3epIML7fjGw

One of the consequences of this is that any initializations that you may have decided to perform only inside onResume () (for example, initializing data from an external source that you need to be fresh), and not in onCreate (), would be uninitialized, if the call to onActivityResult () occurs as part of a restart of the application that was removed from memory by the operating system (since onResume () was not called before onActivityResult ()).

In this situation, onActivityResult () must be prepared to perform such initializations for any such variables that are used by onActivityResult ().

Of course, if the initializations required by the onActivityResult () function can be performed in onCreate (), and not in onResume (), then since onCreate () will be called on reboot before both onActivityResult () and onResume (), this would be The easiest way to get to the materials that you do not need to do every time the application is resumed. If, however, the data that you initialize comes from an external source, and you need it to be fresh, you can initialize such data in both onCreate () and onResume (), while onResume () checks the flag set in onCreate () to see if the data had just been initialized in onCreate), and then update it in onResume () only if they were not. Thus, some of them will always be available (at least from the moment the application was resumed).

Another way to handle this is to store the information returned by the onActivityResult () function in variables that will be retrieved with onResume () and processed there (after any required initializations have been performed onResume ()), instead of doing processing inside the body of onActivityResult () itself.

This feature is very documented, without any explanation or warning (in white papers) about the consequences of this somewhat unexpected sequence. It is also very easy to miss the problem during testing, because on a device with a lot of memory, which doesn’t have many applications running, the activity that causes startActivityForResult () (or its variants) can never be cleared from memory, waiting for it to start working, return the result via onActivityResult (), and therefore all initializations performed by onResume () will already be available, and therefore, the problem may not be detected.

Here is an informative study of some of the problems associated with this sequencing (including a warning about trying to use the Application object application to protect variables from its effects), complete with a UML sequence diagram with drawing:

http://steveliles.github.com/android_activity_lifecycle_gotcha.html

+33
Mar 05 '13 at 1:25
source share

onActivityResult() is called first (just confirmed this with several log statements and looked that onActivityResult() actually called before onResume() )

+9
Jun 24 2018-11-11T00:
source share

One of the consequences of this is that any initializations that you may have decided to perform only inside onResume () (for example, initializing data from an external source that you need to be fresh), and not in onCreate() , would be uninitialized. if the call to onActivityResult() occurs as part of a restart of the application that was removed from memory by the operating system (since onResume() not called before onActivityResult() ).

In this situation, onActivityResult () should be prepared to perform such initializations for any such variables that are used by onActivityResult ().

Of course, if the initializations needed for onActivityResult() can be done in onCreate() , and not in onResume() , then since onCreate() will be called on reboot before onActivityResult() and onResume() , this would be the easiest way Go to materials that you don’t need to do every time the application is resumed. If, however, the initialized data comes from an external source, and you need it to be fresh, you can initialize such data in both onCreate() and onResume() , and onResume() check the flag set in onCreate() , to check if the data in onCreate() just been initialized and then updated in onResume() only if it has not been. Thus, some of them will always be available (at least from the moment the application was resumed).

-one
Nov 09 '16 at 5:38
source share



All Articles