Context and Shared Preferences in Android — An Intro

Image from Unsplash

Context

As the name suggests it’s the current state of an application. It specifies where exactly we are in an application. Example: Activity, Fragment, Intents etc. Context is used at almost everywhere in android development, but we need to understand it correctly how we can use it in a better way so that we can take nice advantage of it.

Usages of context can be but not limited to:

  1. Access Resources
  2. Can communicate with other android components
  3. It can be used to fetch variables that are set in some other place but in same activity or in a different activity if in case of Application Context
  4. To inflate layouts, you can do it using context.

Context can only be used inside an activity.

Now that we have seen a new term Application Context, we need to understand that there are 2 types of contexts. Activity Context, Application Context.

By simple words, it is clear that Activity Context will exist only in a specific Activity which is obviously in running state where as Application Context is a context of application lifecycle. Application Context will be available for all activities under one application until the app dies.

Use Cases for Application Context:

  1. To move data from one activity to another activity
  2. To create a singleton object where it doesn’t depend on any activity
  3. Use Application Context if you require something that should be available on a global scope even after the Context is dead
  4. Application context can be used to make any configuration changes like changing the orientation or to show a toast or a dialog box, even Google Dev Guide uses Application Context for these things

It is always advised to use the context which is nearest to you. In Activity, use Activity Context, when in Application use Application context, when in a Singleton Class use Application Context.

Things to keep in mind while using Application Context:

  1. Unregister/Unbind services after the usage as keeping them open may cause memory leaks and application wears down.
  2. Not all operations are supported with Application Context and some of them are not advised to use Application Context even though it can be used.

Shared Preferences

I don’t know why I wanted to mix up Context and Shared Preferences in the same blog but here we are. So basically, Shared Preferences are used to store any static data to make an application faster without having to read database for non-changing data unless user explicitly changes it. This can be used to store any of the user settings, like Dark Mode or Light Mode, making user logged in without having to login whenever user tries to use the application. Cache would be similar word to understand Shared Preferences in a better way. If you clear cache for a particular app then all your settings are gone and you will be logged out of the app. Shared Preferences store multiple information like int, String, long, an Object etc. Here’s an example of Shared Preferences code -

In the above class, what I’m trying to do is, store a user login status until the user manually logs out of the app. Read and Write the mobile number of the user to use it at various places in my application.

To use SharedPreferences, in I will simply do the following, in my Login Fragment I will fetch the user from database and authenticate and after that I will store login status as true.

That’s it for this blog, hopefully I cover more things in the future. Thanks for reading. Please do comment if you think there is any mistake and more clarification would have been good. Do let me know. Once again thanks for reading and have a good day!

--

--

--

Software Developer | Cloud Computing

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build Android Apps with GitLab CI/CD

Mobile bug hunt

The easiest way to detect and fix memory leaks using Android Studio Profiler Tool and WeakReference…

Themes & Styles in Android

Revamping Meesho’s Android App UI: Implementation Takeaways

First android application??

How to Use Game Service with MVVM / Part 5— Saved Games

Create a Game App with HMS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bhanu Prakash

Bhanu Prakash

Software Developer | Cloud Computing

More from Medium

View Binding in Android.

Migrate and test your RoomDatabase pt. 1

The Easiest Way to Reuse ViewModel Factory using Dagger