When to use static variable and sharedpreference in android. Which is the best to use. Can anyone explain with example. Thanks in advance.
Asked
Active
Viewed 1,092 times
2
-
Right now this part `Which is the best to use` feels like the question will have a lot of opinions. You may want to consider phrasing that part differently. You also may want to see if anyone has written any examples in the new Documenation beta. – Lexi Jul 22 '16 at 11:40
-
2See http://www.tutorialspoint.com/android/android_shared_preferences.htm and http://stackoverflow.com/questions/2475978/using-static-variables-in-android – Sreehari Jul 22 '16 at 11:44
-
static variables should explain themselves. they are static, they never change. so if you want to save info between app stop/start or info that can change, you should use sharedprefs. otherwise, static variables. – cherry-wave Jul 22 '16 at 11:51
3 Answers
2
You can use static when there is not huge amount of data in the application. sharedpreference can be used if the data is more also and can be stored and retrieved as and when it is required.
2
If you stored a value in a static variable and kill the application from recent(pressing Home button and clean all the running apps) then your value kept in static variable will be lost. But if you saved it in shared preference then you can read the saved value whether the application is killed from recent or not.

Atahar Hossain
- 336
- 3
- 11
1
Shared Preferences is used to store and retrieved values.
There are plenty of link available :

Community
- 1
- 1

Ayush Jain
- 144
- 10