Search code examples
androidandroid-intentsharedpreferencesobject-reference

android globally object reference for all activities


May be its simple question, may be its repeated question, this question s not for upvote and all.

I just want to pass my object from one activity to second and second to third activity. I know there are a lot of ways using shared preferences, Intent bundle from one to another activity.

The reason I want to know that why I can't use an object globally for all my activities and if I can how it is possible?

Thanks


Solution

  • There is a handful of ways in which you can achieve this.

    Using a bus/listener:
    https://github.com/greenrobot/EventBus
    https://square.github.io/otto/

    Using the application class (ready the first answer carefully, there are caveats):
    How to declare global variables in Android?

    And using utility classes with static methods.

    Also, if you are have more than one fragment running at the same time, you can create your own interfaces and implement them where you need to receive the information.