I have two activities in my Android application. The main activity launches a thread which will draw an oscillating pendulum, it can be stopped with a "Stop" button. However, if the user moves to the second activity, he should be able to see the the calculated coordinates and parameters of the oscillation (angular velocity, etc.) as they are calculated by the drawing thread in real-time.
My queries are :
How do I send data from the thread performing the oscillation to the second activity ? Note : The data is generated at regular intervals (FPS), hence I need to send the data to the second activity as soon as they are available.
Can we use Semaphores and a Producer-Consumer solution i.e. where The drawing thread produces the value and the second activity launches a thread which consumes them ?
My approach : I have a pendulum class to calculate the required parameters. I have extended SurfaceView to perform my drawing with a thread. So, I am thinking of having two semaphores (producer and consumer), then let the producer thread (drawing thread) update the parameters, and then wait for the consumer(the thread launched by the second activity) to fetch them, and repeat the cycle.
Another idea : Let the drawing thread stop executing when user moves to the second activity, and then spawn two threads (consumer and producer threads) from second activity to perform the task (namely calculate parameters and fetch them), and pendulum implements Parcelable interface so it can be passed to the second activity.
- Are there better/appropriate approaches to this problem ?