Hi to anyone who can answer,
So I came across this question (it was an assignment, I already submitted it anyways). It was regarding shared preferences and explicit intents. I know both can pass data (through putString, putExtra, putInt etc and getExtra, getString, getInt). However which method is better to pass data and why? It can be in terms of functionality or just how much lesser the codes are when comparing each method to each other.
If you want to pass data when transitioning from one activity to another activity, then it's normally better to use intents to pass data.
However if you want the data you are passing to still be retrievable after the user exit your app and reopen it, then you should use SharedPreferences.
Intent is better to use when you are passing data when you are going from one activity to another. Otherwise, you should use SharedPreferences instead. And as mentioned above, if you want the data to be stored and retrievable even when the user reopens the app, then you should always go with a storage option like SharedPreferences.
those are two completely different functionalities that should not be mixed.
Intent extras are used to pass data from one activity to another. If the intention is having one activity put data and the next one receive you should use Intent extras
SharedPreferences is a very lightweight data storage. It's meant to store data on permanent memory and can be retrieved by any entity within your app. If you need data to be saved and access any moment in the future from anywhere in your app, you should use this.
Related
I have encountered a case where the data is being read/written to/from SharedPreferences instead of using Intent to pass it across the Activities. Ignoring the use case argument for using Intents and SharedPreferences for a moment, I would like to know if Android writes an Intent to disk before sending it to the intended Activity.
Because, if it does, there would not be any performance difference between using an Intent and a SharedPreference and I might go with the existing flow with SharedPreference.
So, does it?
Intent is only in memory and absolutely better than SharedPreference, which is formatted to xml and then read/write to disk.
Where shared preferences are stored in disk? (source)
SharedPreferences are stored in an xml file in the app data folder, i.e.
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml
or the default preferences at:
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml
SharedPreferences added during runtime are not stored in the Eclipse project.
Note: Accessing /data/data/ requires superuser privileges
What about Intents? Wait, what is an Intent?
The intent is an "intention" to perform an action; in other words,
a messaging object you can use to request an action from another app component.
It can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
(additional info) In intents, the data to operate on, such as a personal record in the contacts database, expressed as a Uri.
If the above statement does not make an abstract visualization in your mind, simple answer would be
"Intent is just passing of data from one "Class"/"Object" to another and everything resides inside "The Memory" perhaps cache too.
Currently I'am developing app which having a bad API. I have a situation where I have to use data received from network call in a activity, which is 2 steps away from the activity where I made the network call. In other word all the data necessary fill 3 activities comes on this one network call.
So I pass necessary data between activities using Intent Extra. It is not easy to maintain and It takes noticeable time to switch between activities.
I know one possible solution is to store data in database. and access those data from different activities. It feels like a bad practice because no use of those data after user pass 3rd screen.
Is there any better technique to handle this?
You can put all your network logic in the separate class, cache data in some variable and use it in your activities (you can use singleton class or injecting by dagger).
Also you can read about Clean Architecture and get some good practices from it.
If you don't want use anything from above, you can remove data from database after using and not store it forever. Or you can use SharedPreferences if your data is not complex.
I have an Activity A that opens Activity B. During B's lifecycle, it creates lots of data that is important for later use. When I leave Activity B, it gets destroyed. I want that when a user opens B next time, that important data would be restored.
So the question is, how to store that important data?
I had several assumptions:
SharedPreferences (context.getPrecerence(MODE_PRIVATE)).
This is not a good options, because it allows saving only primitive types. I need to save java.io.Serializable object (or at least Parcelable).
Static variable - not an option. I want my data to remain even if JVM destroys my process when the user navigates to some other app.
Context.openFileOutput(). Is this OK to make I/O every time I enter activity/quit it?
Something else?
You can save to SharedPreference using gson.jar. see this answer related to this
You can use a database if the data is user specific.
The database can be accessed whenever user comes back.
Or you can use Bundle (use OnSaveInstance(Bundle)).
This answer is useful in Bundles
If you simply want to retain the data through config changes, onSaveInstanceState(Bundle) is what you're looking for. Otherwise, use a database.
http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState
In my Android Application I have to pass data (Variables regarding the state of the application) from an Activiy to another one. And I have to do this many times in others Activities. What's the best and more efficient way to do this? Should I read that information from the share preferences every time I need it or should I send it as an extra in the intents?
It really just depends on how long you need the data. If you only need it for the lifetime of the application then just pass the data with Intents. This will be the easiest. You can put the data into a Bundle to make passing them around even easier.
If you need it the next time you log in or you need it to be saved if your app is killed for some reason then use SharedPreferences
You also can store it in SharedPreferences and open them up say, in the MainActivity, and pass certain data around. It really just depends on what you need. I hope this helps but if you need a better explanation then please be a little more clear on what you want
In case you aren't familiar with SharedPreferences, the docs have a good example to get you started
SharedPreferences
I think that it depends on why you need this data... If it is some general settings of the application, I think that user preference is better as it persists, but if it is just some data required by the others activities, you should use intent extras.
In my application, I have one activity, which connects to the server and gets certain user information from it. Then, it launches another activity where that information should be processed.
What is the correct way to pass data between activities in Android?
If that data is only needed once, you should use an Intent. Here is a tutorial on it.
If it needs to be accessed all the time, you can put it in an Application subclass or some other singleton.
In general you can use methods like putExtra, to attach data to an Intent, and then use
for example getIntent().getStringExtra(name) to retrieve it in the other activity.