When can I upgrade the user interface?

It seems to me that the documentation in the Activity class should have answered this question, but I'm still not sure - when is it safe to update the activity (or fragment) user interface? Only when action resumes, or any point between start and stop?

For example, the status of Activity docs:

The visible lifetime of the activity occurs between the onStart () call until the corresponding onStop () call. During this time, the user can see the activity on the screen, although he cannot be in the foreground and interact with the user. Between these two methods, you can maintain the resources needed to display activity to the user. For example, you can register BroadcastReceiver with onStart () to track changes affecting your user interface and unregister with onStop () when the user no longer sees what you are showing. The onStart () and onStop () methods can be called several times, as the activity becomes visible and hidden to the user.

From this reading, I would suggest that even if the foreground dialog is displayed, I can safely update the user interface elements behind it.

EDIT To clarify: the reason I ask was bitten by errors when disabling AsyncTask and trying to update the interface in the onPostExecute method. Despite the fact that it works in the user interface thread, the user has switched from this view and I get an exception. Now I am starting a new project and trying to establish some recommendations around the better AsyncTask idioms.

+6
source share
1 answer

I think it comes down to what you mean by a “safe” user interface upgrade. For elements on the Activity screen, they can be updated every time, even if your action is not in the foreground (but be sure to update it from the user interface stream).

However, the disconnect issue is maintaining state: onSaveInstanceState .

As you know, an action that is in the background can be destroyed by the OS to free up memory. Then it will be recreated when you return to it. During this process, the onSaveInstanceState method will be called. If the OS kills the action, any changes made to the state of the user interface after the call to onSaveInstanceState is not saved .

For fragments, you will actually get an IllegalStateException if you try to onSaveInstanceState a FragmentTransaction after onSaveInstanceState . More on this.

To summarize, you can update the user interface of your activity at any time and try to gracefully handle issues with fragments, but you may lose these updates when restoring Activity.

So, you could say that it is really really safe to update an action while it is in the foreground, or rather, before calling onSaveInstanceState .

Edit regarding Async onPostExectue task

This is probably due to the problem that I am talking about above, with the loss of state of the fragment. From a blog post, I contacted:

Avoid doing transactions inside asynchronous callback methods. This includes commonly used methods such as AsyncTask # onPostExecute () and LoaderManager.LoaderCallbacks # onLoadFinished (). The problem with performing transactions in these methods is that they are not aware of the current state of the Activity life cycle when they are called. For example, consider the following sequence of events:

  • The action is performed by AsyncTask.

  • The user presses the Home key, calling the onSaveInstanceState () actions and the onStop () methods.

  • AsyncTask terminates and onPostExecute () is called, not suspecting that the action has since been stopped.

  • FragmentTransaction is executed inside the onPostExecute () method, throwing an exception.

+5
source

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


All Articles