A lot of code is needed to make the query, access the database engine, receive the results, sort them, and put them in your , your UI interaction can be really be bogged down. As you do more and more work that interrupts or shares time with the UI thread, the more the application will tend to skip and lag, and animations will fail to update properly.You might be thinking that once, you read from shared preferences on the UI thread, and nothing bad happened. The user might wonder what is wrong with the app, and the worst case happens if the user is made to wait so long that they get an error message that says, “Application not responding. ” That is the equivalent of you, the developer, suddenly walking around with a giant “Kick Me” sign taped to your back.
This is called context switching, and we’ll start seeing it more as we get into chaining and developing a more complex series of tasks.
Now, I want to talk about a couple of the tools that the Android SDK gives you to deal with the burden of having to manage your own threads.
That sounds very convenient, but the caveat to keep in mind while using this technique is that it is meant for very simple operations that will not get more complex over time.
Let’s suppose that the situation that I just described is not actually adequate for the needs that you have.
But you should keep in mind that if you’re not working with the UI thread, then at some level it is your responsibility to know what threads in your application are active.
The last point I want to bring up about multi-threading is that there is a small time delay each time the execution jumps from one thread to another.A fairly common example of this is trying to make a network call in your application. For a less extreme example, let’s return to the list view idea.Let’s say that instead of populating a static list, you want to query your local database and insert the results of that query into your list.And, in addition to the fact that this is an unmaintainable piece of code, we have a fairly strong design reason to not do this.In this situation, where we have a task that evolves in complexity, there are some other things that we can look at.The responsibilities of the main thread are mainly aimed to draw the UI: it is intended to handle user interaction, to draw the pixels in the screen, and to launch Activities. For example, you could have a list view, build up a list, and add a few items to it.