I have an application that contains a main Activity, which has a very simple screen with all application setup options and a few user selections. The main screen buttons start other Activities.
(1) I'm now implementing a bluetooth scanner into the application and i would like to be able to edit settings and establish a bluetooth connection on the main Activity and pass that connection (via Bundle or Parcelable perhaps?) to the other Activities.
I looked up Bundle as a way to pass the connection, but it appears to only accept primitive and basic types as values. I noticed Intent.putExtra(String, Parcelable) was available, but Parcel sort of confused me. What would be the best way to implement this, or is it even possible?
some relevant code:
//subclass definitions for the Runnables which start the applications in their own threads
private final Handler smsHandler = new Handler();
private final Handler smsOutHandler = new Handler();
private final Runnable smsIntentRunnable = new Runnable() {
public void run() {
Intent smsIntent = new Intent(smsMobile.this, smsActivity.class);
startActivity(smsIntent);
}
};
private final Runnable smsOutIntentRunnable = new Runnable() {
public void run() {
Intent smsOutIntent = new Intent(smsMobile.this, smsOutActivity.class);
startActivity(smsOutIntent);
}
};
//button listeners located in onCreate(), which start the Activities
newTicket.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
smsHandler.post(smsIntentRunnable);
}
});
updateTicket.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
smsOutHandler.post(smsOutIntentRunnable);
}
});
(2) Could I also pass a database connection? I understand that SQLiteOpenHelper must be subclassed, which could complicate IPC, but thought it might still be possible somehow?
There are several options to share data between activities and services.
Update: to expand on your situation a little bit:
You only need IPC/Parcelable when code runs in separate processes. You have to specify in manifest to make code run in separate processes: http://developer.android.com/guide/topics/fundamentals.html#procthread
In your case I asume you activities run in the same process, so you can simply use custom Application class to store/share this data. Make sure it's synchronized
if you access it from different threads. Alternatively you can use any other approach listed in the first link.
You can of course pass database connection within same process, as this is one java application. But you can not pass it via IPC between processes, because all classes need to implement Parcelable in order to pass via IPC and, AFAIK, SQLiteDatabase does not implement parcelable (classes talking to native functions usually don't).