7

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?

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
akash89
  • 881
  • 3
  • 12
  • 31

2 Answers2

2

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?

Community
  • 1
  • 1
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
  • "You may save the changes in a SharedPreferenes or Pass the data using Intent and show the changes before the UI is visible." Fetching the data and storing it is not a problem, problem is how do i populate the UI, without it being visible, you know any call-back methods for that. – akash89 May 27 '15 at 16:51
  • So far its a bad practice and will possibly crash your app by trying to update an UI directly, without currently being visible. You may also use the onSaveInstanceState() to save the state. – Prokash Sarkar May 27 '15 at 17:00
  • onResume gets called before onCreate?! http://developer.android.com/reference/android/app/Activity.html – MikeL Apr 12 '16 at 06:59
2

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.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • Just to summarize your appoarch, I have the data from service/asynchtask while the activity is in background, (but i cannot update the ui, as you suggested), so it would be like, I have the data, application waits for user to bring Activity A in foreground, once that happens, if the updating data is huge, then user will gradually see the ui being populated and not the feel that he gets an already populated ui? Am i correct? – akash89 May 27 '15 at 16:54
  • If the data has finished downloading, it will appear very very quickly on the screen, even if its huge. But yes, the user will be able to see the data getting added to the screen. There's no way to pre-populate the screen in the b/g and *then* show it to the user , as you were planning :) – Yash Sampat May 27 '15 at 16:58
  • Thanks...i think I will take it – akash89 May 27 '15 at 17:01
  • I didn't see why it is not possible to update an activity while it is in background. An activity can be backgrounded but not destroyed (user presses home key to background the app). In this case the backgrounded activity can still be updated by a background thread or task. – Kai Sep 21 '15 at 05:28
  • @Kai: as per my knowledge, what you are suggesting is not possible, above all because Android explicitly forbids UI operations to be performed on another thread as you say. You can try and see if your approach is doable. If you prove it is, I will be happy to be proven wrong. – Yash Sampat Sep 21 '15 at 05:53
  • I was not saying the UI update will be done in a worker thread. Instead, the worker thread can call Handler.post() to update the UI in the UI thread for the activity that's backgrouned. – Kai Sep 22 '15 at 04:34
  • @Kai: and where exactly would you execute that code ? For that to work, you'd have to put the code in either the `onCreate()` or `onResume()` callbacks, neither of which gets called when the `Activity` is in the background ... – Yash Sampat Sep 22 '15 at 04:48
  • The callbacks will be registered in onCreate() of an activity. Then user presses home key to background the app. The activity's onDestroy() will not be called so the activity will not be GCed. In this case, the activity's callbacks can still be called by background threads to update the UI. – Kai Sep 22 '15 at 16:31
  • I agree that it may not be the best practice to update the UI of the activity while it is in background but in general it should not be a problem. As long as application main thread (UI) is alive you can do operations on it. A simple test app can show that even if the activity goes to the background (onPause) called you can still update the UI and once you bring the activity to the front it will display the updated data. – MikeL Apr 12 '16 at 10:20