Skip to content Skip to sidebar Skip to footer

Launch Android Intent From Libgdx Render Method

I'm trying to launch an Intent from the render method in libgdx but I am getting a 'Can't create handler inside thread that has not called Looper.prepare()' error. I have implemen

Solution 1:

The Libgdx "render" thread is not the Android "UI" thread, so when you invoke code in the Android back-end that requires the Android UI thread context you have to jump through some hoops.

In general, the solution is to create a Handler in the context of the UI thread and then post Runnables to that object. This is what the wiki page on integrating android UI elements is doing.

If you have the Toast implementation working correctly, then the Intent code should also work (both have the same requirement to be run on the Android UI thread context).

Perhaps there is some other problem with the Handler you're creating (is it not being created during the Libgdx create callback? (Its implicitly associated with the thread that created it.) Or are you invoking this code too early in your setup? A full backtrace might provide more details.

Solution 2:

I have now got it sorted. It turned out I needed to interrupt the render thread then I could launch the intent. I changed implementaion slightly as I call an AlertDialog and launch the intent from there. I have accepted the answer as it got me thinking along the right lines.

Post a Comment for "Launch Android Intent From Libgdx Render Method"