NYRA Giveaways for Saratoga Race Meet

All giveaways are free with paid admission, while supplies last. Season pass or tick plan holders are guaranteed to receive each premium Saratoga giveaway if they are enter Saratoga Race Course…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Different ways to get Context in Android

Context is one of the important and most used property. You need Context to perform a lot of things on Android. Be is displaying a toast or Accessing database, you use context a lot while building Android app.

So, it is important to know different types of Context and methods you can call to get context. Lets get started.

The this keyword in general sense refers to current class instance. So, when use “this” keyword inside an Activity, it refers to that Activity instance. And as Activity is subclass of “Context”, you will get context of that activity.

If you are not directly inside Activity, for example inside an OnClickListener, you can get the current context by referencing with Activity name like MainActivity.this (Java) or this@MainActivity (Kotlin)

This method can be called on a View like textView.getContext(). This will give the context of activity in which the view is currently hosted in.

If you need to access resources which are out of scope of specific activity, like when accessing SharedPreferences, displaying Toast message etc. you can use this.

So unlike activity context, which will be destroyed when close an activity, Application Context the application wide context which won’t get destroyed until you completely close application.

You can directly access application context by calling getApplicationContext() or by calling on activity context like context.getApplicationContext()

This method is only useful when you are using ContextWrapper. You can get the original context which was wrapped by ContextWrapper by calling contextWrapper.getBaseContext()

(ContextWrapper is a wrapper class, using which you can override any method of Context, while still retaining the original context)

When you call getContext() from an Fragment, you will get the context of the activity in which that fragment is hosted in.

You can get the parent activity from a Fragment by calling getActivity().

These methods are same but “NotNull” versions of getContext() and getActivity() respectively. Usually, if a fragment is detached from Activity, you will get “null” value when you call getContext() or getActivity(). So even when you are sure the context won’t be null, you still have to add null checks (especially in Kotlin) because they return Nullable type.

But requireContext() and requireActivity() will throw IllegalStateException instead of returning null, if there is no context.

So, did you learn something new? If you did, please clap and share the post. 😄

Add a comment

Related posts:

Converting .arf files to .wav files

I had to convert audio of a video to text and do the later processing. Since I was working on my office system I don’t have admin access and this is the first barrier I faced. And from here all I had…

Why does Costco have the lowest price for gas?

From my personal experience — Every time I need to fill gas, I think of Costco and plan my itinerary around it. I am amazed Costco has shaped the decision-making and plans. All the Costco warehouses…

Mutable VS Immutable Objects in Python

Object-oriented programming (OOP)is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that…