views:

50

answers:

2

I have an application in which I am going to be ransferring data via a bluetooth connection.

In my proof of concept app, I was able to put the bluetooth communication in a different thread and keep all of my other work on the same activity (using ViewFlipper).

However, for my next iteration, I will need to use several different "screens" (activities). The flow would go something like this:

My initial activity will connect up to the Bluetooth device I will be getting the data from.

One activity off of the "main" activity will be to get a list of data from the BT device and display it in a list view. Then as I click on an item I will need to get a more detailed view of the data (which is shown in yet ANOTHER activity but also has to access the BT connection).

There will be additional, specific activities off of the "main" activity.

So my question is how is the best way to manage that, where I initiate the bluetooth connection ONCE and don't have to initiate it again for each activity?

Thanks.

A: 

Store it as a static field in Application subclass (and instantiate with application context if required).

alex
A: 

As Alex said store a controller for the connection as a member of a subclass of a custom application class.

You have to make your own application class that extends the android application class and register this class in your manifest. Now you can get the Application with getApplication and cast it to your subclass. Now you can access the member variables of your Application class.

Some other possibilities are described in the android documentations.

I would not use a static field in your application class. There is only one application class anyway and you can be sure that the whole application object won't be destroyed while your app is running. Some authors of Android books state that this is not that certain for static variables.

Janusz