1

I'm new in android and I have a question.

I want to have just one instance of a class in my whole android program so that it's attributes won't change during the program, but I also want to call it's methods in all my activities.

With some search I realized that I can pass my object via implementing class as Serializable or Parcelable. I did this but I got following error:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object
java.io.NotSerializableException: microsoft.aspnet.signalr.client.hubs.HubConnection

as you see one of my class attributes is HubConnection which is in microsoft package and i can't make it Serializable.

What can I do to pass the object of SignalR class to another activities? And what are my options?

public class SignalR implements Serializable {

    private HubConnection connection;
    private HubProxy proxy;

    //some methods
}
Sina
  • 275
  • 5
  • 15

2 Answers2

4

If you want a single instance of YourCustomClass throughout the application, you can do by keeping a reference of your custom class object in YourApplication class.

Create a class and extends it to Application class. Create setter and getter method in your application class to access the your custom class instance. Now you can access the your custom class instance from any where within your app and you don't have to pass the instance between activities.

public class YourApplicationClass extends Application{
    private YourCustomClass yourCustomClass;

    public YourCustomClass getYourCustomClass() {
        if (yourCustomClass == null) {
            yourCustomClass = new YourCustomClass();
        }
        return yourCustomClass;
    }

    public void setYourCustomClass(YourCustomClass yourCustomClass) {
        this.yourCustomClass = yourCustomClass;
    }

}

Don't forget to put android:name="YourApplicationClass" in your manifest file.

<application
        ......
        android:name=".YourApplicationClass"
        ....... >

Now to access the object from your activity, say MainActivity you would write something like -

@override
 protected void onCreate(Bundle savedInstanceState) {
    YourApplicationClass app = (YourApplicationClass) getApplication();
    YourCustomClass yourCustomInstance = app.getYourCustomClass();
}
Rahul Chaurasia
  • 1,601
  • 2
  • 18
  • 37
  • thank you. this worked for me well. i also tried singleton pattern and that worked too. what is the difference between these two? which one is better? – Sina Nov 12 '15 at 07:39
3

If your goal is to have a single instance of your class globally accessible from all your activities, you do not want to be passing it around with bundles. Rather use a singleton pattern.

If you do need to use the bundles for some other reason, use Parcelable instead of Serializable, it is meant to be faster. There is a pattern to follow with creating a parcelable. Your best bet is to copy-paste the answer from here, and change the constructor, parcelling, and unparcelling.

Community
  • 1
  • 1
Kevin
  • 1,626
  • 16
  • 30
  • I just want to add - be careful with using singletons. They are easily destroyed by the garbage collector if your app is in the background. If you are interested, [read more](http://geekswithblogs.net/AngelEyes/archive/2013/09/08/singleton-i-love-you-but-youre-bringing-me-down-re-uploaded.aspx) – yennsarah Nov 12 '15 at 06:49
  • Very true. My `getInstance` usually does a null check and re-instantiates if required. – Kevin Nov 12 '15 at 06:52
  • use gson library and send your custom class's object as if they were string, very neat and clean – Pankaj Nimgade Nov 12 '15 at 07:31
  • In this case he wants to reference a connection. I don't think storing strings is quiet what he is looking for. – Kevin Nov 12 '15 at 07:37
  • @Kevin thank you. updated my goal on title. i used singleton pattern and worked for me. i also tried the other answer and that worked too. do you know which one is better? singleton pattern or extends Application? – Sina Nov 12 '15 at 07:47
  • The general consensus is to avoid singletons, though I personally feel that it depends on your needs. If the state of the instance does not change and creating a new one does not affect anything, then i prefer singletons. This post explains why the solution in application context is generally better, http://stackoverflow.com/questions/3826905/singletons-vs-application-context-in-android. – Kevin Nov 12 '15 at 07:54