Solution 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
Solution 2 :
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
Solution 3 :
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
Problem :
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
Comments
Comment posted by a_local_nobody
why does it seem inconvenient
Comment posted by Kanwarpreet Singh
Depends upon your use case, If it’s not required further in any other activity then there is no need to allocate memory to it and store it into SharedPrefrence.
Comment posted by Rafsanjani
You can use a global object which is accessible by both activities.
Comment posted by EpicPandaForce
The real trick is that you don’t have to use 2 Activities for 2 screens.
Comment posted by Nicola Gallazzi
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