Question

I need to write an Android application that can receive UDP packets and, for each one of them, display a new object on the screen with OpenGL.

I have tried the following: My OpenGL renderer class has an ArrayList of objects that are rendered every frame and I created a function that adds new objects to this list when called.

In my main activity, I have created a new thread to deal with the incoming UDP packets. Every time a new packet is received, a message is sent to the handler who calls the function that adds a new object to the list.

This works at first, when the packet is received, the object is displayed, but if I press BACK and start the application again right away, the new objects are not displayed anymore even though the packets are being received (I can tell through the log).

Is this the right way to go but I am missing something or is there a better way to achieve the expected result?

Was it helpful?

Solution 2

I found what was causing the problem. The network thread was the problem. It would continue running after the application was closed and therefore would update the wrong list. I had to make the thread stop when the onDestroy() was called.

To fix it, I added some "stop" flags to break the while(true) loop but that still wouldn't work well, so the trick was to close the socket that was waiting for data (s.receive()).

OTHER TIPS

I managed to fix the problem. I had to declare the list of objects as static, I am not so sure why. It seems like after the Activity was destroyed and restarted, there was some confusion between the list being modified by the "add" function and the list of objects being rendered.

It would be nice if someone could explain what was going on. Thanks!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top