Thursday, May 11, 2023

Daily#4. Android's Threading Model and Looper

 Android uses a single-threaded model for UI operations, meaning all UI updates must happen on the main thread. The Looper class plays a crucial role in this model.

 Looper is a part of Android's message passing framework. It's designed to keep a thread alive and to process Messages and Runnable objects from a MessageQueue.


 Here's how you can use a Looper in a background thread:

class ExampleThread extends Thread {
    public Handler handler;

    public void run() {
        Looper.prepare();

        handler = new Handler();

        Looper.loop();
    }
}

Remember: Don't perform long-running operations on the main thread to avoid UI freezing. Use a background thread with a Looper for such tasks!

No comments:

Post a Comment

Learning Journey #6: Brief Exploration of Databases and its Management Systems

  Welcome to the "Learning Journey" series. This space is my personal archive where I share insights and discoveries from my explo...