Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it safe to update the UI?

Tags:

android

I feel like this should have been answered by the documentation in the Activity class, but I'm still not positive -- when is it actually safe to update the UI of an Activity (or a Fragment)? Only when the activity is resumed, or any point between being started and stopped?

For example, the Activity docs state:

The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying. The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.

From that reading, I would assume that even if a foreground dialog is displaying, I can safely update UI elements behind it.

EDIT To clarify: the reason I ask is having been bitten by errors when firing off an AsyncTask, and attempting to update the UI in the onPostExecute method. Even though it runs on the UI thread, the user has navigated away from that view and I would receive an exception. I'm starting a new project now and trying to establish some guidelines around better AsyncTask idioms.

like image 468
Thorn G Avatar asked Jun 12 '14 17:06

Thorn G


1 Answers

I guess this comes down to what you mean by "safe" to update the UI. For elements on the Activity screen, they can be updated whenever, even if your Activity is not in the foreground (but make sure to update from the UI Thread).

However, the issue that will trip you up is saving state: onSaveInstanceState.

As you may know, the Activity that is in the background may be destroyed by the OS to free up memory. It will then be re-created when you come back to it. During this process, the onSaveInstanceState method will be called. If the OS does destroy the Activity, any changes you made to the UI State after the call to onSaveInstanceState will not be persisted.

For Fragments, you will actually get an IllegalStateException if you try to commit a FragmentTransaction after onSaveInstanceState. More info on that.

To summarize, you can update the UI of your activity at any point and try to gracefully handle the Fragment issues, but you may lose these updates when the Activity is restored.

So, you could say that it is only truly safe to update the Activity while it is in the foreground, or rather, before the call to onSaveInstanceState.

Edit in regards to Async Task onPostExectue

This is likely related to the issue I am referring to above with Fragment State Loss. From the blog post I linked to:

Avoid performing 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 have no knowledge of the current state of the Activity lifecycle when they are called. For example, consider the following sequence of events:

  1. An activity executes an AsyncTask.

  2. The user presses the "Home" key, causing the activity's onSaveInstanceState() and onStop() methods to be called.

  3. The AsyncTask completes and onPostExecute() is called, unaware that the Activity has since been stopped.

  4. A FragmentTransaction is committed inside the onPostExecute() method, causing an exception to be thrown.

like image 120
jacobhyphenated Avatar answered Oct 22 '22 11:10

jacobhyphenated