2

When to use static variable and sharedpreference in android. Which is the best to use. Can anyone explain with example. Thanks in advance.

3
  • 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. Commented Jul 22, 2016 at 11:40
  • 2
    See tutorialspoint.com/android/android_shared_preferences.htm and stackoverflow.com/questions/2475978/… Commented Jul 22, 2016 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. Commented Jul 22, 2016 at 11:51

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

1

Shared Preferences is used to store and retrieved values.

There are plenty of link available :

1.Shared Preferences

2.Shared Preferences

1 Comment

Static Variable makes your program memory efficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.