Android Lifecycle Details

The main action in my application sometimes calls startActivityForResult, waiting for a result that tells it (the main action) what information to display next. Looking at the documentation on the life cycle of the process, it seems that when the activity of the choice is active, the main action is considered a β€œbackground” activity and can be killed.

So what happens when the selection action completes? I see that my activity will be recreated, and onCreate is called using the SaveInstance Bundle package, but what then? Is onActivityResult called as if my main activity never went out and recreated?

Also, is there a way to force this behavior in a test environment, as otherwise it would be a very rare occurrence?

+4
source share
3 answers

Hint: magazine statements

The status is paused as described in the docs:

If the activity has lost focus but is still visible (i.e., a new, not full-sized or transparent activity focuses on the activity), it is suspended. The stopped activity is completely alive (it stores all the information about the states and members and remains attached to the window manager), but can be killed by the system in the extremely low memory of the situation.

This means that under normal circumstances, your main action should simply pass onActivityResult() when the selection action is complete.

However, the documents also state that:

Background activity ( activity that is not visible to the user and paused ) is no longer critical, so the system can safely kill its memory recovery process for another foreground or visible processes. If his process should be killed when the user goes back to action (make it visible on the screen again), his onCreate(Bundle) will be called using savedInstanceState it was previously set to onSaveInstanceState(Bundle) so that it can reload itself in the same state in which the user left him.

In such cases, the main action can be redrawn.

It is important to note that the docs never mentioned onActivityResult () as one of their lifecycle methods here

Thus, it can also happen that the android system considers sub-activity and parent activity (reads startActivityforResult() and onActivityResult() ) in the same way as it processes the activity dialog as here:

Visible activity (activity visible to the user, but not in the foreground, for example, one after the foreground dialogue) is considered extremely important and will not be killed if it is not to support the work of the foreground.

+1
source

The answer is basically yes: activity is recreated, and control passes through onCreate (), onActivityResult (), onStart (), etc. Activity is also destroyed if the user rotates the device, for example. from portrait to landscape, unless the application has explicitly prevented this behavior. So just rotate the device (CTRL-F11 on the emulator) to check.

0
source

Suppose there are two actions A and B and Activity A triggers activity B through startOnActivityResult (intent, 200) , then your activity goes to background and activity B (onCreate, onStart, onResume) depends on what you redefined.

Whenever your B call causes completion (), your activity B is destroyed, and action A comes to the fore. In this case now, i.e. Activity A, the call will be onActivityResult β†’ onStart β†’ onResume, but your onCreate will not be called, because it is called only when a certain Activity is called.

Suppose you did not call finish () from action B and call action A through intent, then you will only call onCreate ().

Also onActivityResult () is very useful when you want to preserve the value of your spinners or want to notify datasetchanged () your ListView of your first activity after the third activity event. You just need to check your resultCode from Activity in onActivityResult and complete your actions.

0
source

Source: https://habr.com/ru/post/1389652/


All Articles