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.
Related
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 need to pass an ArrayList across activities and make it persistent.
From a popular SO post i found the solution. But still I cannot understand why SharedPreference from API 11 takes a Set
SharedPredferences.getStringSet but not a so common ArrayList directly.
What are the logic reasons, ex. as to avoid duplicates using a Set for instance?
If you only need to persist simple flags and your application runs in a single process SharedPreferences is probably enough for you. It is a good default option.
There are two reasons why you might not want to use SharedPreferences:
Performance: Your data is complex or there is a lot of it
Multiple processes accessing the data: You have widgets or remote
services that run in their own processes and require synchronized
data
You should can use Bundle to share list to another activities. If it is a globally used stuff keep it in your Database(Sqlite)
With reference of this, Concept behind SharedPreference is build a user interface for your app settings. you can only add key-value type of data in shared preference.
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.
I have couple of applications that implements some login logic. If lets say one application is logged to some_account#gmail.com I want that all of these applications be logged to some_account#gmail.com. If I logout I want to all application do the same. But I don't want to immediately do the same. Application itself can handle it, but it need to know if some other application is already logged in and if yes just log in for the same email address as this app. So I need to know what is the email address for which other app is logged. I need to store one string.
First I was thinking about SharedPreferences, but this is rather bad idea because there are other options (and stackoverflow is full of bad example of SharedPreferences usage between processes). Despite it I tried this. Set up sharedUserId on all apps, called createPackageContext and eventually try to get preferences. But I cannot read from it. Always I got null, even if I used Context.Mode_WORLD_READABLE - which is deprecated by the way.
Ok, lesson learned do not use SharedPreferences for that (I suppose). But everything what I need now is to store single string somewhere where it could be read by other my apps.
Maybe I should use ContentProvider? But seriously... for one string?
What is the other option? I am sure that for so simple operation I really don't need Service or ContentProvider, but I actually haven't got idea how to do that.
You could use a broadcast receiver. All you would have to do is send a broadcast to application B when the data changes in application A. Then application B can handle the data in the background, and store it however you need to. It might be a bit of over kill, and there could be a better way to do it, but it would work.
Given that a Service may be running even when the launching activity is destroyed,
and also that data is passed usually using the extra bundle along the intent,
I wonder if the Service is able to directly access the activity's preferences.
My guess is no, it cannot. But it would save me writing a chunk of code if it can do it.
Assuming that You're asking about SharedPreferences. If so, then documentation is clear (by link above):
Note: currently this class does not support use across multiple
processes. This will be added later.
So, currently, if Your service is running in a separate process it's not possible. In that case I would suggest to store Your preferences not in a SharedPreferences, but in some base which could be accessible via specific ContentProvider.
P.S. Actually I tried access SharedPreferences from multiple processes and it has worked (at least in my case), but I've decided to use another way because of the documentation mentioned in the answer.
If you mean SharedPreferences then you can just call getSharedPreferences with the same name.