Android Activity.runOnUiThread is not static, so how can I use it?

For example, if I have a thread making expensive stuff, and from this thread I want to run runOnUiThread in the Main (activity) class. Obviously, I should not make an instance of my activity class (Main). Therefore, if I try

Main.runOnUiThread(mRunnable); 

from my thread this gives me an error saying that this is not a static method and therefore it cannot be accessed in my way. Now I understand that the activity class is almost almost available in a static form.
How can I do it?

(Btw: I do this because I get a CalledFromWrongThreadException, only the original thread that created the view hierarchy can touch its views)

+4
source share
6 answers

You must use the Handler class. The handler class runs in the user interface thread. When you finish work in your thread, call handler.sendEmptyMessage() , from where you can make changes to your ui.

 private final Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { // make changes to ui } } 
+3
source
Raunak has the right idea. I will simply add that you can also specify an integer in the sendEmptyMessage method as the identifier of the handler. This will allow you to create one handler that can handle all your UI updates, for example.
 public static final int EXAMPLE = 0; public static final int ANOTHER_EXAMPLE = 1; private final Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { switch( msg.what ){ case EXAMPLE: //Perform action break; case ANOTHER_EXAMPLE; //Perform action break; } } } //Call to submit handler requesting the first action be called handler.sendEmptyMessage(EXAMPLE); 

Hope this helps!

+3
source

Your question does not contain enough details, but because of the sound of things, you are in a private inner class (Runnable?) In your activity (Main). If so, you can either write:

 Main.this.runOnUiThread(mRunnable); 

or

 runOnUiThread(mRunnable); //will see that there is no runOnUiThread in the current class and begin looking "upwards" 

In addition, you can look at AsyncTask , in particular, onPostExecute , onPreExecute and onProgressUpdate that run in the user interface thread.

+3
source

first create runnable outside onCreate. Like this:

 private Runnable myRunnable = new Runnable() { @Override public void run() { //work to be done } }; 

and then call runnable using:

 runOnUiThread(myRunnable); 
+2
source

For those looking for an easy instant solution, follow the simple steps.

  • Make your class reference before your onCreate() method

     MyClass obj; 
  • Initialize it in your onCreate() method

     obj = MyClass.this; 
  • Call runOnUiThread()

     obj.runOnUiThread(new Runnable() { public void run() { //perform your UI tasks here } }); 

Hope this helps.

0
source

all of the above answers are not very correct.

1), if you want part of the code to be launched in the user interface thread from any stream code base. you can do: Looper.getMainLooper (). Post (new Runnable (...))

because Looper.getMainLooper () is a static variable and initialized in ActivityThread.

2) if your executable code fragment is inside the action then you can use:

MainActivity.this.runOnUiThread (...)

0
source

All Articles