0
votes

I'm working with a Bluetooth app, and I have a thread listening to input messages, and 2 activities: main menu, and bluetooth console. I'm making the connection in the menu Activity, and then I need to pass some objects to the console Activity:

  • a thread reference to use its write() method
  • the Context from main Activity
  • and a boolean I need to check eventualy

I read about using Intent.putExtra and passing a serializable o parceable class object. I wrote a class with the objects I need to pass, but I don't know how to do this, or even what serializable and parceable means.

Isn't there any easy method to pass these objects from an Activity to another?

2
Activities have their own context, so you won't need to pass that. getApplicationContext() - sgarman
True. But i still need to access to that thread methods and the boolean - Roman Rdgz

2 Answers

1
votes

You could extend Application and put the shared objects into that class. Something like:

public class MyApplication extends Application {
    private boolean myBoolean;

    public boolean getMyBoolean() { return myBoolean; }
}

You can put anything here you want to use a global variables.

To set the Application of your app to MyAPplication, use the AndroidManifest.xml

<application ... android:name=".MyApplication">
0
votes

In short answer. No. Activities are isolated from each other because they can span processes so the only portable option is to serialize them. But you can use libraries like Flexjson to serialize plain old Java Beans across using Intent.putExtra(). Much simpler than hand writing serialization with Parcels. Check out: http://flexjson.sourceforge.net

There is a bug in Android that you have to do the following:

/** Fix for Android bug http://code.google.com/p/android/issues/detail?id=5697 */
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());