-1

I want to share object from one activity to another activity. I know of 2 good ways:

  1. Using bundle: By making object's class implement Parcelable, can pass object in bundle via intent.
  2. Using singleton pattern: saving the object instance in this class and then fetch it where ever required.

Which of the above mentioned is better or recommended way? Kindly also tell if there is some other better way.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • There are multiple ways - you could also write to some form of persistent storage (SQLite, SharedPrefs), upload to a server, etc. - but it really depends on the specifics of your use case. – PPartisan Feb 25 '19 at 16:10
  • You should only use Singleton if you are completely aware of the implications: https://stackoverflow.com/questions/49046773/singleton-object-becomes-null-after-app-is-resumed/49107399#49107399 – EpicPandaForce Feb 25 '19 at 16:11
  • @PPartisan : I have to fetch data from backend api, json response using retrofit, Should I store data in sharedPreference? Is it ok to store json response in sharedPreference if size is less than 1 MB? What are the disadvantages of storing data in shared preference? Your support would be appreciated!!! – Robin Sharma Mar 02 '19 at 11:41

1 Answers1

0

Singleton isn't a good ideia on Activities, because it will produce memory leak (redundancy of information of an Activity).

Parcelable it's a good idea, because you will not use disk IO or Network when you create a local object from a form in the first Activity.

If you retrieve an object from Network or local database, you don't need anyone, because you can retrieve the object in the next Activity.

Marcus
  • 773
  • 8
  • 11
  • I have to fetch data from backend(network query using retrofit). Fetched data would be required on multiple fragments in the application. Should I store data using singleton or use sharedPreference to store object? – Robin Sharma Mar 02 '19 at 09:19