Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

executing runOnUiThread on a separate class [duplicate]

Tags:

java

android

Possible Duplicate:
Android how to runOnUiThread in other class?

My Asyn Classes are a separate class file.

public class AdamTask extends AsyncTask<String, Void, String>{
         public void showToast(final String toast)
        {
            runOnUiThread(new Runnable() {
                public void run()
                {
                    Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
                }
            });
        }
}

How would I execute this method in my AsyncTask Class? I am getting an error The method runOnUiThread(new Runnable(){}) is undefined for the type AdamTask

new AdamTask(Eve.this, How to pass the eve activity here).execute();

like image 964
theJava Avatar asked Oct 26 '12 11:10

theJava


People also ask

How do you use runOnUiThread in non activity class?

You need your Activity object to do this. Pass your Activity's this reference through the constructor and use it in your AsyncTask. Since runOnUiThread is a public method in Activity class, you cannot use it in some other Custom class or class that extends other than Activity itself.

How does runOnUiThread work?

Basically what runOnUiThread() will do is - Runs the specified action on the UI thread. It will check the current thread and if it finds its the MainThread it will execute that task immediately , otherwise first it will switch you to app MainThread and then it will execute the given task.


2 Answers

Just typecast the context to Activity class

((Activity)context).runOnUiThread(new Runnable()
    {
        public void run()
        { 
             Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
        }
    });
like image 75
user936414 Avatar answered Oct 02 '22 05:10

user936414


You need to have the Activity's reference (lets name it activity) and pass it to your AsyncTask class. Then you can call runOnUiThread like this:

activity.runOnUiThread

The runOnUiThread is a method defined in Activity class.

Just add a contsructor to your AsyncTask. Your AsyncTask will look like this:

public class AdamTask extends AsyncTask<String, Void, String> {

private Activity activity; //activity is defined as a global variable in your AsyncTask

public AdamTask(Activity activity) {

    this.activity = activity;
}

public void showToast(final String toast)
    {
        activity.runOnUiThread(new Runnable() {
            public void run()
            {
                Toast.makeText(activity, toast, Toast.LENGTH_SHORT).show();
            }
        });
    }

...

}

Then to call the AsyncTask you need something like this:

AdamTask adamTask = new AdamTask(Eve.this);
adamTask.excecute(yourParams);

UPDATE As Sam mentioned in the comments, AsyncTasks may result in context leaking when configuration changes occur (one example is when screen rotates and the Activity is recreated). A way to deal with this is the headless fragment technique.

Another way, more efficient, is using an event bus. See here for more information (link provided by Sam in the comments).

like image 21
Angelo Avatar answered Oct 02 '22 06:10

Angelo