Search code examples
androidviewbackground-process

android change UI from background thread - why it's working?


Will an application ever crash when changing text, background resource and inflating some views in it in a background thread, if the view's visibility is GONE? I've done some tests with two buttons, one visible and one gone, and when I changed text of the visible one in a background thread, it crashed, and when I changed the text of the button with the GONE visibility, it worked without a crash. Can someone explain this?


Solution

  • Ok, I have a something for you.

    Event Handling and Threading

    The basic cycle of a view is as follows:

    1. An event comes in and is dispatched to the appropriate view. The view handles the event and notifies any listeners.

    2. If in the course of processing the event, the view's bounds may need to be changed, the view will call requestLayout().

    3. Similarly, if in the course of processing the event the view's appearance may need to be changed, the view will call invalidate().

    4. If either requestLayout() or invalidate() were called, the framework will take care of measuring, laying out, and drawing the tree as appropriate.

    Note: The entire view tree is single threaded. You must always be on the UI thread when calling any method on any view. If you are doing work on other threads and want to update the state of a view from that thread, you should use a Handler.

    You can find much more here http://developer.android.com/reference/android/view/View.html .

    In your case Your View is already GONE so I think its not attached to View Tree.