When I want to do something in background, if the action is very simple: Like do “something in background” and then update the UI, instead of using an AsyncTask I’m considering to use (just for faster coding):
new Thread(){
public void run(){
final ArrayList<myObjects> objects= myDatabase.queryObjects();
runOnUiThread(new Runnable() {
@Override
public void run() {
updateUIWith(objects);
}
});
}
}
But I really don’t know if using “final” objects that way can result in memory leaks or have other kind of troubles.
Is using this method OK?
finalonly says to the compiler that you won’t reallocate theobjectsvariable in your code. There is no link betweenfinaland memory leaks.If you use an anonymous inner class (the
new Runnable...) you have to makeobjectsfinal.I am not very familiar with Android but if your
updateUIWith()method does interact with UI objects, it might be an issue as I would assume UI updates need to run in a specific UI thread. If you don’t update UI objects in that method then you code should be fine.