Context and Shared Preferences in Android — An Intro
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:
- Access Resources
- Can communicate with other android components
- 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
- 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:
- To move data from one activity to another activity
- To create a singleton object where it doesn’t depend on any activity
- Use Application Context if you require something that should be available on a global scope even after the Context is dead
- 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:
- Unregister/Unbind services after the usage as keeping them open may cause memory leaks and application wears down.
- 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.
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!