Reputation: 105
I am not sure if this code can cause a memory leak.
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mButton.setText("Hey!");
}
});
}
});
thread.start();
In the thread I use the method from the activity runOnUiThread() and I keep a reference to the button in the Runnable object.
If the activity is destroyed before the thread finishes it will keep a reference to the activity because we will reference a method from the activity and a view in the runnable.
Could you clarify me if I'm right about that? If so, how can I solve this problem?
Upvotes: 0
Views: 218
Reputation: 40
yes, this will cause a memory leak and if u use Thread.sleep() it blocks the whole thread you should use coroutines instead of thread.
Upvotes: 1