Keeping a Bluetooth connection through actions on Android

Possible duplicate:
Android: How to transfer a Bluetooth connection to another action?

I have an application in which I am going to transfer data via a Bluetooth connection.

In my proof of conceptual application, I was able to put the Bluetooth connection into another stream and save all my other work in one action (using ViewFlipper).

However, for my next iteration, I will need to use several different β€œscreens” (actions). The stream will look something like this:

My initial activity will be connecting to the Bluetooth device from which I receive data.

One of the activities of the "main" activity will be to obtain a list of data from the BT device and display it in a list. Then, when I click on an item, I will need to get a more detailed view of the data (which is shown in another OTHER activity, but also needs to access the BT connection).

From the "main" activity, additional specific actions will be performed.

So my question is how best to manage this when I initiate the ONCE Bluetooth connection and do not need to start it again for every action?

+4
source share
2 answers

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

You must create your own application class that extends the Android application class and registers this class in your manifest. Now you can get the application with getApplication and apply it to your subclass. Now you can access the member variables of your Application class.

Some other features are described in the Android documentation .

I would not use a static field in your application class. In any case, there is only one application class, and you can be sure that the entire application object will not be destroyed while your application is running. Some authors of Android books claim that this is not so important for static variables.

+1
source

Save it as a static field in a subclass of Application (and create an instance of the application if necessary).

-1
source

Source: https://habr.com/ru/post/1314075/


All Articles