Best way to prevent UI from freezing

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Best way to prevent UI from freezing

Postby joshhendo » Fri Mar 20, 2009 12:54 am

Hi Everyone,

Firstly, I will quickly intorduce myself (as this is my first post here). My name is Josh Henderson, and I am currently located near Sydney, Australia. Currently in my last year of school (finishing up in 2 terms), with one of my subjects being Software Design and Development (though it isn't much help except for theory), and have always been interested in programming and development all my life. I am starting to look in developing for phones, especially on Android.

Hmm, not heaps, but I thought I would give everone just a small introduction to who I am!

Anyway, onto my question. I have started making my first Android application, and it is going well and as I would except. One problem that I knew I would have, though didn't bother to look into it until now, is that when it fetches something from the web, the UI freezes up (I know why this is, though I am looking for the best way to fix this).

Basically, I am fetching a XML file (dynamically generated) from my webserver and then working with the information received. I am fine with all that except for the UI freezing up when it is fetching it (it can be slow on some phones, and the webserver isn't particulary fast due to something outside of my control). What is the best way of doing this?

I know that I could use multi-threading, but they are the one thing that I have always hated working with. I have tried UI development with wxWidgets/wxPython in Python, and am still having great trouble implementing a GUI with threadding, as something gets missed or some error comes about. If threadding is the best way to go, how should I go about doing it? I haven't had much experience with Java (mainly python and C# (I need C# for my Software Design course at school, I don't plan to continue using it afterwards)), and am not farmiliar with threading concepts.

If there is another way to overcome this problem without threading my application, that would be great. I would really like a "loading" screen in the mean time, and then switch to the view with all the information.

Any help is greatly appreciated.

Thanks, Josh.
Posts: 4
Joined: Fri Mar 20, 2009 12:42 am
Location: Sydney, Australia


Postby MrSnowflake » Fri Mar 20, 2009 9:35 am

Threading is the ONLY way to do this (well not really, you could read bits and pieces from the xml stream from the ui thread every loop it makes but that's even harder to do)! A loading screen (assuming you want to use the ProgressDialog) can only be used (ie. not giving ANR (application not responding)) when the ui thread is free, so if you are fetching stuff in the ui thread then the ProgressDialog will also hang and you still will get the ANR dialog. And the same goes for other languages too, like you said yourself, ui's without threading tends to make them hang and unresponsive.

So the only useful way to implement this is, unfortunately for you, threading. Though this is by far _NOT_ an item you should avoid! Threading will make your life in many case a lot easier (but also more complex at times :) ). First of all 1-up your java threading skills. These practices are also valid for other language like c#, c++, python (I suppose), ... So learn to get threads right one time will allow you to use them in hundreds of languages.

Then you should get your xml from the web in the thread, parse it in this thread and notify the ui (using Activity.runOnUiThread()) when new info is available. When further questions come, I'll be happy to help you out, no go and make them threads :).
User avatar
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium


Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests