0

I have a activity with a Listview and a adapter attached to it. I have a class which syncs data and hold it. (I think we should not care about from where data is coming) and it runs on a different thread. Now I want to know the clean way to update adapter but We should not call any function of activity from that class as it runs on different thread.

One way I know is to create handler in activity and pass it to other class and use it from there. But I want to know if activity is in background then activity's function can be called by UI thread or only when activity come in foreground.

I do not want to miss any update and want to update activity when it is in foreground.

user1875798
  • 263
  • 3
  • 6
  • 16

4 Answers4

1

If you want to update your listView from another class you can have a few ways to do that.

  1. Send broadcastIntent from your worker class and add BroadcastReceiver to your activity and when you receive the right message, update your listview.

  2. Second way is to create private or public class in your Activity which extends AsyncTask and in your doInBackground() do your work and in onPostExecute(result) update your listview.

  3. Third way which I can imagine, but I don't think it's the best way create a static method is your activity which you can use from any other class for updatiogn your UI.

The best thing which you can use here at least in my opinion is AsyncTask.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
hardartcore
  • 16,886
  • 12
  • 75
  • 101
0

this code will do what you want:

         runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //your actions
                }
         });
Royi
  • 745
  • 8
  • 22
  • If I want to call some function of activity inside run method I need to have reference to activity object, which is similar to have handle on UI thread and requesting a runnable to that. But I don't want to call activity function if it is in background , I want function to be executed only when it comes in foreground because there can be N different activities I don't want to update all, I want to update only foreground activity and other should get call when they come in foreground. – user1875798 Jan 24 '13 at 10:49
0

We should not call any function of activity from that class as it runs on different thread.

That's wrong. (in java an object is not running in a thread. What you can say is that a method is running in the thread from which the method was called)

In Androïd (and in most UI frameworks) the rule is this:

You can only call a method updating UI from the UI thread.

If you have some code running on a thread (not the ui thread) and if that code need to update the UI : you can use the Handler of the UI-thread to post UI update code to the UI-thread. If the activity is not in the foreground when you post something to update it's UI : nevermind! the code you just post will be executed at some point in the future.

ben75
  • 29,217
  • 10
  • 88
  • 134
0

You should use AsyncTask, take a look at http://developer.android.com/reference/android/os/AsyncTask.html

Joao Guilherme
  • 387
  • 2
  • 10