Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug android application with break point in Runnable [Android Studio]

I need to debug my application and debug the code is part of Runnable and do some actions in application then debug the code again, but I can't do it, I can't do any actions as debug point is fired immediately.

code :

public class UpdateHandler {

    public static Handler getHandler() {
        if (sHandler == null) {
            HandlerThread looper = new HandlerThread("update Handler");
            looper.start();
            sHandler = new Handler(looper.getLooper());
        }

        return sHandler;
    }
}


UpdateHandler.getHandler().post(new Runnable() {
    @Override
    public void run() {
        update();
    }
});

public void update() {
    // i put the debug point here .
}
like image 963
Moaz Rashad Avatar asked Dec 20 '22 04:12

Moaz Rashad


1 Answers

For runnables/async tasks etc it is useful to call Debug.waitForDebugger(), prior the code you set the breakpoint on:

...
Debug.waitForDebugger();
foo.bar();               # breakpoint on this line
...
like image 103
Marcin Orlowski Avatar answered Dec 28 '22 08:12

Marcin Orlowski