3

I'm using a library for usb communication written by someone else. I'm using one activity to find the correct usb device and make the appropriate driver object and I want to use that driver in another activity (the why of this is a bit complicated).

Unfortunately, the driver object doesn't implement parcelable or serializable so I'm not sure how to do this. So I have two questions

1) What options are there for passing the driver object between activities?

2) Even if I succeed in question 1, will I run into problems because the receiving activity hasn't been given explicit permission to use the usb object?

ryan0270
  • 1,135
  • 11
  • 33

5 Answers5

2

Apart from the options below (and the other options, such as Web storage - discussed here, or JSON options), there is no way to send data from one Activity to another. You should either reconsider how you are doing what you are trying to do, or consider using a different Driver.

If the code is open source or open licensed, you may consider hacking in Serializable or Parcelable by extracting the source and modifying it to fit your needs. More on decompiling Android source is available here.

There are several methods you can use for sharing content between two Activities in different projects:

1. SharedPreferences, SQLite, Serialization, or Content Providers. These will all require you to break down your Driver Object into simple types. More on storage can be found in the docs.


2. Parcelables can be shared via Intent between Activities.

There are several methods you can use for sharing content between two Activities in the same project:

1. You can use SharedPreferences, SQLite, or Serialization. More on storage can be found in the docs.


2. You can set it to a static variable. For example, have a Store class where you save static variables:

public class Store {
    /** provides static reference to the driver */
    public static Object driver;
}

Then to set from anywhere, just do:

Store.driver = myDriver;

and to get from anywhere, just do:

Object driver = Store.driver;

3. Create a custom Application class and set this in your Android Manifest. This application can store the driver, and doesn't necessarily have to be static. More on this can be found at Extending Application to share variables globally.


4. The third option is to create a singleton accessor to your Activity. So, in your activity that has the driver referenced, add the following class variable:

private static MyActivity self;//replace MyActivity with the name of your class.

Then, add the getter:

public static MyActivity sharedMyActivity() {
    return self;
}

Finally, add this line in onCreate (after the call to super.onCreate(...)):

self = this;

Now to access your driver (we'll say it has a getter), just call this from anywhere:

Object driver = MyActivity.sharedMyActivity().getDriver();

As for part two of your question - if you are attempting to read from and write to a hardware device in an Activity that does not provide USB permissions, this will not work.

Community
  • 1
  • 1
Phil
  • 35,852
  • 23
  • 123
  • 164
  • Thanks for the suggestions, but don't those all require the activities to all be within the same application? In my case, that's not an option. – ryan0270 Aug 13 '13 at 23:15
  • @ryan0270, yes, My mistake. I have updated my post to include both cases, and discuss your options, as well as part 2 of your question at the bottom. – Phil Aug 14 '13 at 00:12
1

I guess I am a bit late for party but it nags me that nobody mention using of Service.

Activities can just bind to service and give it instructions and service will pull up connection/communication job.

Ewoks
  • 12,285
  • 8
  • 58
  • 67
0

1) You can create a singleton object to save all your cross-activity data and store objects there. It's quiet simple in realization.

2) Permissions are granted to a whole application, not a single activity.

As far as your activities belong to different applications, there is no way for them to communicate by using shared memory.

Maxim Efimov
  • 2,747
  • 1
  • 19
  • 25
0

You can use fastJSON to easily serialize your data into json string and then restore back to object. All you need to do is to create setters and getters for your driver object.

For the permission part, you have to ask from user when he install the app containing the activity. No way to skip it.

coderek
  • 1,860
  • 14
  • 20
0

You can convert object in to json format and pass as a string, and on receiving side you can again convert this json string into object. For that you can use gson library. https://code.google.com/p/google-gson/downloads/list

Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57