Skip to content Skip to sidebar Skip to footer

Android: Difference Between Getmainlooper() And Looper.mylooper()

I'm now trying to resurrect one project. There was an exception on getMainLooper()... I thought that may be there's a problem with MainLooper initialization and added Looper.prepar

Solution 1:

The difference is that Looper.prepareMainLooper() prepares looper in main UI thread. Android applications normally do not call this function. As main thread has its looper prepared long before first activity, service, provider or broadcast receiver is started.

But Looper.prepare() prepares Looper in current thread. After this function is called, thread can call Looper.loop() to start processing messages with Handlers.

So, in your case you had two threads - X and Y. The X thread is the main UI thread that has its looper already prepared by Android. When you are in Y thread and you're calling Looper.prepareMainLooper() you're trying to prepare looper in X thread (main thread). This fails because X's looper is already prepared. But when you call Looper.prepare() in Y thread, you're actually preparing looper in Y thread and therefore ready to call Looper.loop().

Post a Comment for "Android: Difference Between Getmainlooper() And Looper.mylooper()"