I wanted to share an object(has multiple states) between 3 activities and any of the activities can modify the object state and the same state should be accessible to all 3 activities. I do not want to use singleton object as it will exist for whole application. Shared object should only persist till the time these 3 activities are present.
-
You can set a singelton object to null if you wish as any object and it won't last for whole application. Another answer is to use observer pattern or EventBus library to send and receive info from other activites or fragments. – Thracian Sep 16 '17 at 18:21
-
Thanks for the answer. Can you tell me how will observer pattern help here? – Rahul Sep 17 '17 at 05:02
-
EventBus is a library that uses Observer pattern. You can search for EventBus Observer pattern and you will get lots of articles or examples. You can implement observer pattern by implementing an interface(listener callback) on each activity and adding these activites to list of listeners via Application class(name register(MyCallback callback)). Yeah, it should be Applcation for listener list or observers not to be null. onStart add(register) an Activity to list and onPause remove(unregister) an activity. This is a common pattern used widely as you may know if you know Observer pattern. – Thracian Sep 17 '17 at 06:42
4 Answers
hi @rahul you can implement parcelable in your object and pass it via Intent to all activities.In this way you can maintain states as well as avoid singleton.
Since making a object parcelable involves more boilerplate code you can use this parceler library which needs no boilerplate code. After making a object parcelable you can pass that to any activity via Intent like this
Intent intent = new Intent(this, MyActivity.class);
intent.putParcelable("key", object_you_want_to_pass);
...
startActivity(intent);

- 428
- 4
- 12
If your object can be serialized, maybe you could try to use SharedPreferences
to store your specific object, using one of the methods described here. You can then parse it and modify it from any activity.
And if you want it to be persistent only when all of your three activities are running, just clear your SharedPreferences entry.

- 127
- 1
- 11
In general designing your app you should avoid creating such shared states when it's possible. But if you really need it - see below.
You could create a singleton with kind of "reference counting" - Self releasing (reference counting) singelton
If you want to limit its visibility you can make it package private for instance (if your Activities are in the same package).
But this whole thing looks a bit overcomplicated to me, I'd probably go with normal singleton or a field in your Application class, maybe removing it manually if I know at some time that it's no longer needed.

- 5,600
- 3
- 34
- 37
Singleton is still the best way to manage this.
class Singleton {
static private Singleton _instance = null;
static private boolean _activity1 = false;
static private boolean _activity2 = false;
static private boolean _activity3 = false;
static public register(Activity activity) {
_instance = null;
if (activity instanceOf Activity1) _activity1 = true;
if (activity instanceOf Activity2) _activity2 = true;
if (activity instanceOf Activity3) _activity3 = true;
}
static public unregister(Activity activity) {
_instance = null;
if (activity instanceOf Activity1) _activity1 = false;
if (activity instanceOf Activity2) _activity2 = false;
if (activity instanceOf Activity3) _activity3 = false;
}
static public Singleton getInstance() {
if (_activity1 && _activitiy2 && _activity3) {
if (_instance == null)
_instance = new Singleton();
return _instance;
}
return null;
}
}
class Activity1 {
@Overrride
public void onCreateView() {
Singleton.register(this);
}
@Override
public void onDestroy() {
Singleton.unregister(this);
}
Apply same to Activity2 and 3. Above are only pseudo codes.

- 1,563
- 1
- 9
- 13