3

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 :

  1. 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.

  2. 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.

  1. Are there better/appropriate approaches to this problem ?
a_pradhan
  • 3,285
  • 1
  • 18
  • 23

2 Answers2

2

My suggestion would be to use one Activity with two Fragments. Than you don't have the problem to manage your threads and the passing of the data. Let both Fragments communicate with Activity using callbacks.

Another approach would be to use a Service for the calculation of the data and pass'em to the desired Activity. This can be done with callbacks too or using a Binder.

Both setups are up to the same idea, to have a model and two views. The advantage is that your Thread for the drawing and calculating don't have to die because of the Activity switch. Furthermore you will have up-to-date data in any of your Views and control over your Threads at any time.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
0

Send the data in the intent you use to start the 2nd activity. Except use startActivityForResult()

Intent i = new Intent(this, SecondActivity.class);
i.putExtra("sentData","lorem ipsum");
startActivityForResult(i, 1);

That way you can pass data back and forth.

See this answer for a great example that I think will help immensely: https://stackoverflow.com/a/10407371/794088

Then manage the pausing of drawing in your first activities onPause() and likewise the starting it up again in onResume()

Community
  • 1
  • 1
petey
  • 16,914
  • 6
  • 65
  • 97
  • I don't think I require to send data back-n-forth across the activities. I am currently trying the following : Implement parcelable for my pendulum class. Spawn two threads from the second activity where one calculates the result, the second fetches it (producer-consumer case I guess) and display them. – a_pradhan Aug 09 '13 at 20:21