Search code examples
androidmultithreadingrunnable

Android Runnable find run caller


I have inherited an enormous and horrible Android project that reminds me of this post.

In this project there is a mysterious Activity that implements Runnable. The run function is failing because the Activity is not correctly initialized. My problem is I have no idea where the Runnable is actually run.

How can I find the point where the Runnable interface of this Activity is called? Breakpoints just seem to show the parent Thread calling run() - wow, surprise.


Solution

  • My problem is I have no idea where the Runnable is actually run.

    If the Runnable is used in a Thread, there should be code like new Thread(someObject) that you try to find.

    The constructor expects a Runnable and if you remove the implements Runnnable part from the class, the code that creates the Thread is now incorrect and results in a compiler error that should tell you what you look for.

    In case someone is calling the run method directly you can rename it and the compiler will tell you which places call a method that does not exist. Alternatively

    Exception e = new Exception("Debug");
    e.fillInstackTrace();
    e.printStackTrace();
    

    is a quick way to show the call-hierarchy in code without using a debugger or an advanced IDE like Eclipse. That's sometimes nice to put in a problematic execution branch in the method to find who is causing that.