Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharedPreferences application context vs activity context

I am using several SharedPreferences to store data in my app. Some preferences are used in a lot of activites.

I know that the SharedPreferences are internally backed by a map for fast read-access and written to sdcard when settings are changed.

I wonder which way is better if a sharedpreference is accessed by a lot of activies:

  1. Instantiate it in every activity using the activity context.
  2. Instantiate it in every activity, but using the application context.
  3. Put it in e.g. the Application class and instantiate it only once there, similar to a singleton.

If I use 1. solution is there a sharedpreference object for every activity? And will the sharedpreference's internal map get destroyed when the activity is destroyed?

If I use 2. solution will there be only one instance although I call getSharedPreferences in every activity? And will the internal map be in memory as long as the application is alive?

Hopefully someone knows how Android handles it internally.

like image 971
d1rk Avatar asked Jul 19 '12 18:07

d1rk


People also ask

What is the difference between application context and activity context?

Application Context: It is the application and we are present in Application. For example - MyApplication(which extends Application class). It is an instance of MyApplication only. Activity Context: It is the activity and we are present in Activity.

What is the difference between activity and context?

In android, Context is the main important concept and the wrong usage of it leads to memory leakage. Activity refers to an individual screen and Application refers to the whole app and both extend the Context class.

What is the difference between activity context and application context MCQS?

Question 18: What is the difference between Activity context and Application Context? (A) The Activity instance is tied to the lifecycle of an Activity. while the application instance is tied to the lifecycle of the application.


Video Answer


2 Answers

It is worth reviewing the sources that show that a Context instance (be it an Activity or an Application instance) share the same static map HashMap<String, SharedPreferencesImpl>.

So whenever you request an instance of SharedPreferences by the same name via Context.getSharedPreferences(name, mode) you get the same instance since it first checks if the map already contains SharedPreferences instance for a key (which is the passed name). Once SharedPreferences instance is loaded it will not be loaded again, but taken from the map instead.

So it actually does not matter which way you go, the important thing is to use the same name in order to get the same prefs from different parts of the application. However creating a single "access point" for the prefs could be a plus. So it could be a singleton wrapper over the prefs instantiated in Application.onCreate().

like image 173
Vit Khudenko Avatar answered Oct 09 '22 03:10

Vit Khudenko


SharedPreferences are managed internally by Android as singletons. You can get as many instances as you want using:

context.getSharedPreferences(name, mode);

as long as you use the same name, you'll always get the same instance. Therefore there are no concurrency problems.

like image 24
David Wasser Avatar answered Oct 09 '22 04:10

David Wasser