I’m using Eclipse to develop an Android app to plot Bluetooth data. The primary components are a Bluetooth Activity, a background thread created by the Bluetooth Activity, and a Plotting Activity.
From Logcat I know for a fact that the background task continues to run and produce BluetoothData even after I switch the activity from Bluetooth Activity to Plotting activity.
I found the method in the background thread that writes BluetoothData to Logcat. So I decided to use this method to pass data data to the Plotting Activity’s plotData() method as follows:
PlottingActivity plottingActivity = new PlottingActivity();
plottingActivity.plotData(BluetoothData);
But it does’t work with the call to plotData() being a regular instance method. Why?
It doesn’t work because with this form (perhaps terrible form) of invoking plotData() the onCreate() method of the PlottingActivity activity does not get called. This results in at least in a null pointing exception for redraw() method so plot doesn’t redrawn or drawn for that matter.
So that’s why it doesn’t work. But what makes it work? Interested? Making plotData() a static method. Works great. Clips right along in real-time. Looks like an Oscilloscope.
But I have received negative feedback about this solution (may it’s not thread safe or something) I should now ask for alternative solutions. [NOT A DISCUSSION]. Just real, practical, sensible alternatives to using a background thread and an instance method.
You can’t transfer data from one activity to another, as only one activity can ever be active at a time. The reason it’s working with static members is because the class definitions exist, but the objects created that were referenced by the activity no longer exist; they were garbage collected when the new activity came about.
You need to create a service that stays active no matter which activity is active.