سؤال

Android advises always that long operations should be done on background threads. However, the background threads are not allowed to manipulate the UI.

What I'm trying to accomplish is building a UI from an XML definition. I already parse the XML on a background thread, but adding the views, and settings their attributes is really not possible on that thread.

Is there anything I can do to make the app keep responsive while the remaining UI manipulation tasks are done?

Note: I haven't tested how much time this manipulation takes. I just assume it is much, because a lot of control is going to be added, and a lot of property is going to be set.

Edit: I've already managed to do the parsing in the background, so I know how to create threads, communicate between them. I'm curious if can do anything to have the UI be more responsive, when I'm FORCED to run many things on the UI thread.

هل كانت مفيدة؟

المحلول

I don't know what kind of UI you are trying to build, but it really shouldn't take all that much time. Try it first, optimize later.

If performance really turns out to be a problem, and your UI is modular in any way, try to use ListViews or GridViews to compose it. They will only build the Views needed to fill the screen, and recycle them to build new components as you are scrolling down and up the screen.

نصائح أخرى

You can use two things :-

  1. Worker Threads
  2. AsyncTask

They both are capable of updating the UI from a different thread. Both are explained in this link Threads

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top