0

Lately I've read this comment on Reddit:

I think EventBus on android is popular because people don't know how to share a java object reference between android components like a Fragment and an Activity, or 2 Activities and so on. So basically I think people don't know how 2 Activites can observe the same object for data changes which I think comes from the fact that we still don't know how to architect our apps properly.

As far as I know:

  • directly reference values

    ((HostActivity)getActivity()).someValue

    may cause problem such as memory leaks or NullPointerException

  • Callbacks or other observer pattern

    EventBus,inner class like listener and etc

So I was curious that whether there is any other ways , could you share with me on it?

Sinyuk
  • 277
  • 6
  • 16
  • I think [this](http://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities) is an excellent answer – Fady Saad May 03 '17 at 03:18

1 Answers1

0

imo, there are few ways out of your opinion such as:

  • use local db, now ORM like Realm is really popular, its also safe way.
  • use share preferences if your object is able to convert to json, its up to your situation.
  • use intent with serialize/parcelable object bundle.
  • use a singleton, now @scope with Dagger 2 is good choice.
  • use RxJava as event bus.
linhtruong
  • 441
  • 3
  • 12