Reputation: 454
I have MainActivity
and my separate class, name is MyClass
. I want to run some code in MyClass
. And I have methods for that in MyClass
. In the constructor of MyClass
I am passing Context
of MainActivity
class and converting that Context
in Activity
.
MyClass.java
private Context context;
private Activity activity;
public MyClass(Context context) {
this.context = context;
this.activity = (Activity) context;
}
public void MyFunction() {
activity.runOnUiThread(new Runnable() {
public void run() {
// Code
}
});
}
Everything is going fine still here. In the runOnUiThread
, I have written some code. There is a situation, After executing runOnUiThread()
body, I want to go back to MyClass context. I want this to be done in runOnUiThread()
only. Is there any way to switching back to current thread again?
Thanks
Upvotes: 0
Views: 4427
Reputation: 1497
You can use one of the answers posted by ρяσѕρєя K or Kushal. Here are my 2 cents to make things a tad clear.
All the code inside runOnUiThread
will be executed on UI thread and it can not be split into two parts; one running on UI thread and another in a different thread/context. Now, if you want to execute a code-block after runOnUiThread
completes then you can modify your runOnUiThread
function as below (as suggested in some of the answers here):
public void MyFunction() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// Add code to run on UI-Thread here
}
});
// Add code to run on non UI-Thread here (i.e. on the calling Thread)
}
Upvotes: 4
Reputation: 10288
If you define and implement a custom class in the activity class, unless you create a new thread, all of your code will run in the UI thread.
The runOnUiThread
method passes the information necessary to have a different thread reference the UI thread for updating views or other UI components.
To run code in your original class you should do a few things. First, you should define it as a separate class (in another file, not as a private class).
Second, you should pass it the Activity
(maybe as a WeakReference
) and then use the Activity
method "runOnUiThread" when necessary.
Another option is to create a Handler
or Thread
to run off the UI when you want it to run.
You are not entirely clear about your use-case...
Upvotes: 1
Reputation: 1821
What I got from question is that you need to run some code in UI thread and some in background thread. I yes, you may modify MyClass
to use Handler
as following.
private Context context;
private Handler backgroundHandler;
private Handler uiHandler;
public MyClass(Context context, Handler handler) {
this.context = context;
// this.activity = (Activity) context;
this.uiHandler = handler;
HandlerThread handlerThread = new HandlerThread("MyClass.Handler");
handlerThread.start();
backgroundHandler = new Handler(handlerThread.getLooper());
}
public void MyFunction() {
uiHandler.post(new Runnable() {
public void run() {
// Code to execute on ui thread
backgroundHandler.post(new Runnable() {
public void run() {
// Code to execute on bg thread
}
});
}
});
}
Pass the Handler
as:
MyClass myClass = new MyClass(context, new Handler());
Upvotes: 1
Reputation: 8508
Once you access APIs for PubNub from runOnUiThread()
, android system will switch the processing to your current thread automatically and executes outer statements :
public void MyFunction() {
activity.runOnUiThread(new Runnable() {
public void run() {
// Code which runs on MAIN UI thread
}
});
// once upper part is done, switching to current thread
// these statements will be execute on current thread
// statement 1;
// ...
}
Upvotes: 1