Snake
Snake

Reputation: 14648

Android and threads

I have 2 questions in terms of android development and threads

1) When do you think I should use threads in android development?

2) If I have the main UI thread waiting on some variable to be set before it displays a toast, then I thought about having a while(true) loop in a sperate thread that keeps checking that variable. Now if the variable is set, how do I call the method on the first thread (The UI thread) that will display a toast

Thank you so much

Upvotes: 0

Views: 147

Answers (4)

MByD
MByD

Reputation: 137282

Using threads directly is not necessary in most cases. The android facilities for threaded programming are great, and easy to use.

You have about three options to call the UI thread from another thread:

  • Handler - set an handler on the other thread from the UI thread, and send it a message when need.
  • AsyncTask - Perform the main task in background, but modify the UI before, during and after completion.
  • PerformOnUiThread - call this method with a runnable to modify the UI on the UI thread.

Good article to read about is Painless Threading.

Upvotes: 2

haythem souissi
haythem souissi

Reputation: 3273

You must use thread, if you want to prevent application from the current error or crash "Application not responding". i use it when i call web services, geolocations.

Good frensh article:

http://souissihaythem.blogspot.com/2011/08/application-not-responding.html

http://souissihaythem.blogspot.com/2011/08/application-not-responding-solution.html

Upvotes: 0

starvingmind
starvingmind

Reputation: 300

Depending on the type of app you are working on/with, proper use of Theads can be key to having a fast and responsive UI.

Key Rule:
If you need to to anything that accesses the filesystem, network, image loading, or anything that requires more than simple value or state checks, immediately launch and perform that effort in a thread. As already stated here, a number of options are available, although myself I usually use:

change_UI_if_needed_to_indicate_click_response();
new Thread(new Runnable(){ 
    do stuff here ; 
}).start() 

You can also use standard (non-inline) thread objects like this.

That may be bad practice, I don't know. Anyway, it works well for me, and isn't really the point of your question.

If you want to run something on the UI thread using standard Java, instead of specialized Android object that specifically allows for such things, there is a post(Runnable) method attached to each and every view. For me, that's the easiest my to get code back onto the UI thread.

myView.post(new Runnable{
   codeForUI();
});

My general rule is to try to never run anything on the UI thread that does not need to be there. Hope that helps.

Upvotes: 1

lrAndroid
lrAndroid

Reputation: 2854

Never have a while(true) loop that continuously runs. It'll burn massive amounts of resources and, in your case, accomplish very little.

Threads are good to run for (mainly) background tasks and resource intensive tasks (so that you don't block the UI thread). To create a new Thread, use the following:

new Thread(new Runnable(){
    public void run(){
       //do stuff here.
       }
    }
).start();

You can also look into the Android AsyncTask to avoid using Threads directly.

Upvotes: 1

Related Questions