akash89
akash89

Reputation: 891

Can I update the UI of an activity which is in background?

The question is more conceptual than coding-related.

I have an app with Activity A and B.

I call an AsyncTask from Activity A, while the call is being made, I don't want to block the user showing the progressdialog, so my user can freely move around the application without getting bored of waiting.

Now, the query is AsyncTask or lets say a Service is being called from Activity A which is responsible for downloading some kind of data from the server. While the call is being made, the user has changed the Activity and Activity A has gone to background.

Now, while the user is freely moving around the application, he just wants to come back to Activity A to check the download status,(which is something lets say I set some data to my TextView).

Now the question is, once the download is over , while my Activity A is still in background, my UI should be updated while Activity is still in background. This way the user feels he gets the data before he switches to Activity A.

Is this possible, if so how? Summarizing my question, can I update the UI of an Activity while it is still in background with the Asynctask or Service which the Activity invoked to fetch data from server.

One post suggested that ideally I need to update the **UI in onResume(). My question is, is this the only approach?

Upvotes: 7

Views: 3890

Answers (2)

Prokash Sarkar
Prokash Sarkar

Reputation: 11873

onResume() would be the best approach. You may save the changes in a SharedPreferenes or Pass the data using Intent and show the changes before the UI is visible.

Another approach would be running a service and checking if the activity is visible. If its visible immediately update the UI or wait until user visits the activity. To check if the activity is currently visible see here,

How to check if activity is in foreground or in visible background?

Upvotes: 2

Yash Sampat
Yash Sampat

Reputation: 30601

once the download is over , while my Activity A is still in background, my UI should be updated while Activity is still in background. This way the user feels he gets the data before he switches to Activity A.

It isn't possible. You see, the Activity has to pass through the onCreate() and onResume() callbacks for the UI to be changed.

Also, you should be using a bound Service to update the Activity when it returns to the foreground.

Upvotes: 2

Related Questions