0

It seems very inconvenient to make my object serializable or parcelable for onActivityResult resp. intent.getSerializableExtra() or intent.getParcelableExtra(). So I would easily store it into SharedPreferences.

Related to Android: Sending an object from one activity to other activity

Fl0r3
  • 64
  • 4

3 Answers3

1

Amit pandey, i agree with that

In case if you want to pass data from activity 1->2 2->3 3->4 you use simple intent if you want to use get data from 4->3 and 3->2 and 2->1 than you use startActivityForResult.

But, I think the shared preferences are commonly used to store any data that you want to restore after the app closes. if you're going to share data while it's open, you should consider the use of ViewModels instead

Barral
  • 61
  • 4
  • I agree with you, shared preferences should not be used to share not persistent data across the app. ViewModels with a shared activity context would be the right choice to share data across fragments – Nicola Gallazzi Feb 27 '20 at 16:09
0

In case if you want to pass data from activity 1->2 2->3 3->4 you use simple intent if you want to use get data from 4->3 and 3->2 and 2->1 than you use startActivityForResult. But in case if you want to send or get data in any activity or Fragment its good if you use shared preference or local storage like sqlite

Amit pandey
  • 1,149
  • 1
  • 4
  • 15
0

From my point of view, SharedPreference acts like local storage on your phone. It works well if you want to save some state like true or false, as simple as possible. Why? Because saving and get the data from SharedPreference quite painful.

Example use of SharedPreference. If you want to show a banner to the user, in a condition that the user never closes it before. So, if the user closes it before, it won't be shown to the user. You can save that state whether the user had close it before or not in SharedPreference. That's my perspective of using SharedPreference

Now, for onActivityResult. I usually use it if I want to get a variable that, usually, had modified in other activities. And that variable is important, for example, to keep update to the user about the data he/she been modified before.

Example use of onActivityResult. Let's say you move from activity A to activity B, which carried some value with it. In an activity B, the value is being modified. And when the user back to activity A, from activity B, it's being carried too. That value is now used in activity A to show important info, which links to what user doing in activity B

Hope this answer helps you. Sorry for my bad grammar. Peace

bobby I.
  • 101
  • 1
  • 11