android Looper and ui thread

755 Views Asked by At

I'm investigating cross platform library published by dropbox. following code is from it. I have questions (1)it makes handler which is connected with main looper. I have heard that this kind of way we can create ui thread handler. but does it has any relation with original ui thread(Activity ui thread) or it really creates another seperated ui threads?

if there are 2 ui threads, then it it possible that one ui thread access another ui components and modify its ui?

public class AndroidEventLoop extends EventLoop
{
    Handler mHandler;

    public AndroidEventLoop()
    {
        mHandler = new Handler(Looper.getMainLooper());
    }

    public void post(final AsyncTask task)
    {
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                task.execute();
            }
        });
    }
}
public abstract class EventLoop {
    public abstract void post(AsyncTask task);
}

and it called in Activity

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    EventLoop mainThread = new AndroidEventLoop();
1

There are 1 best solutions below

0
On BEST ANSWER

No it's not creating a second main thread. All tasks that are posted to AndroidEventLoop are processed on the Main Thread. This for example, enables you to do changes in the UI after data was processed on a background thread.

http://developer.android.com/reference/android/os/Looper.html#getMainLooper() https://blog.nikitaog.me/2014/10/11/android-looper-handler-handlerthread-i/