Reputation: 18065
I'm writing an Android app that connects to a Bluetooth device, reads in data, and graphs it in the main Activity using AChartEngine (this entire part works).
I want to include a second Activity, which contains a WebView (powered by PhoneGap). When a button is pressed in the main Activity, the second Activity should open while the initial one continues to be alive in the background and the AChartEngine graph continues to be updated.
My current code approaches this problem using an AsyncTask, which works fine before the app starts accepting and graphing data over Bluetooth. After that happens, when I press the button to open the second Activity, the entire application crashes.
How do I show one Activity in the foreground while manipulating another Activity in the background? I've considered that the limit on the number of AsyncTasks open could be causing this issue, since I create a new AsyncTask for every data point I receive in order to add it to the graph asynchronously, but the number open at any time seems to be well under the limit.
Here are the relevant parts of my current code:
public class viewer extends Activity
{
/* ... */
public void handleButtonPress() {
// called when our button is pressed
new StartWebview(viewer.this).execute();
}
protected class StartWebview extends AsyncTask<Void, Void, Void> {
public Activity activity;
public StartWebview(Activity activity) {
this.activity = activity;
}
@Override
protected Void doInBackground(Void... values) {
Intent i = new Intent(viewer.this, WebActivity.class);
activity.startActivity(i);
return null;
}
}
protected class NewPoints extends AsyncTask<Double, Void, Void> { // called each time we receive a new data point
@Override
protected Void doInBackground(Double... values) {
mCurrentSeries.add(values[0], values[1]); // x, y
if (mChartView != null) {
mChartView.repaint();
}
return null;
}
}
}
public class WebActivity extends DroidGap
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
Upvotes: 0
Views: 138
Reputation: 6731
Once an activity is no longer shown, it entirely possible for Android to destroy it completely.
You should be doing your data collection from a foreground service putting the data into a persistant data store like a contentprovider backed by sqlite database.
Then use a loader to fetch the data and provide it to your charting tool. The content provider will be notified each time new data arrives in the database automatically.
This might sound overkill, but you'd be loosing all your data and potentially crashing anyway even on a simple screen rotation.
Upvotes: 2