Does latent activity mean that the activity object does not exist?

enter image description here

I watched this page with “Android Programming” on a large ranch ranch, and I was confused by the suggestion below. It says that "when the action is hidden, the object of activity does not exist." This bothers me, because when I open the application and press the home button, onPause () is called, and the action reaches the suspended state with its activity hidden in the OS, as shown in the figure. It should still be running in the background because onDestroy () is not called when I click the home button. In fact, when I open my task manager, I see what the activity looked like when I clicked the Home button. What exactly do they mean by "the object of activity does not exist?" when is it clearly paused in the background?

+5
source share
2 answers

The THeir documentation looks a bit wrong and confusing. The official documentation on Android does not say about the state of “hidden”, it will be the block “Application process killed” in the documentation here

Basically, from a STOPPED state, the OS can destroy your activity at any time. If this happens, onSaveInstanceState will be called. Then the Activity variable will be invalid. At any time, it can recreate a new action and call onCreate, and then onRestoreInstanceState on it, passing in the bundle you saved earlier to recreate the activity.

0
source

This bothers me, because when I open the application and press the button on the house, onPause () is called, and the action reaches the “paused” state with its activity hidden in the OS, as shown in the figure.

I would not say that it is "hidden". Your activity is alive and healthy, just stopped.

It should still be running in the background because onDestroy () is not when I click the home button.

It is right. When you press the home button, you say to the operating system, "Hey, I’m going to another place, but I don’t necessarily do it."

If you click the back button instead, you will see onDestroy . Here you say to the operating system, "Well, I’ve finished this activity, doing what you want with it."

What exactly do they mean by "the object of activity does not exist?" when is it clearly paused in the background?

The hidden state is entered when the OS needs to destroy your activity without "telling" it about it (for example, by clicking the "Back" button). This happens when your device goes through a device configuration change. A classic example is rotation. This also happens if the OS needs to free up memory. In this case, onSaveInstanceState will be called to capture the state of your activity in the Bundle . The hidden state is the preservation of this Bundle and the class name of your activity. The actual Activity object is marked for garbage collection. Then, given only the Bundle object and the class name of your activity, the OS can create a new instance of your activity if the user returns to it.

One of them is that you do not need to execute onSaveInstanceState yourself if there is no specific information that you want to keep. Some components of the view hierarchy will automatically add information about themselves in the Bundle, because the super Activity.onSaveInstanceState will still be called. From the docs:

The default implementation provides most of the user interface for each instance for you by calling onSaveInstanceState () for each view in the hierarchy with an identifier and saving the current identifier (all of which are restored to the default ofRestoreInstanceState (Bundle)). If you override this method to save additional information that does not fall into each individual view, you will most likely want to turn to the default implementation, otherwise be prepared to save all the state of each view yourself.

http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

You can check the source here: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/app/Activity.java#1371

-1
source

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


All Articles