Home » Android » android – What is the better way to get Context?

android – What is the better way to get Context?

Posted by: admin May 14, 2020 Leave a comment


According to this answer or the android’s documentation there is several ways to get the Context in an app and pass it to an other class/method/whateveruneed.

Let’s say I’m in the Foo Activity and in need to pass the context to Bar’s constructor.

Bar bar  = new Bar(Foo.this);
Bar bar2 = new Bar(this); //same as first i guess
Bar bar3 = new Bar(getApplicationContext());
Bar bar4 = new Bar(getBaseContext());
Bar bar5 = new Bar(MyApp.getContext); // get context statically 

Taking into account of memory leaks, speed , general performance , what will be the better way between all those possibilities ?

How to&Answers:

You should check out this question – which basicly covers the same as yours.

Also the Developer Docs on Avoiding memory leaks gives you a decent explanation of some situtations in which various of the methods are reasonable to use.


I think that this post will provide you enough information. Look at the first response.

Difference between Activity Context and Application Context


You would probably want to use this. It is the Context of your current Activity (Which is a context) and has shortest lifecycle. But be aware of the memory leak that could occur.


I have not any direct answer to your question.But if you compare Foo.this and this then better to use first one as sometimes (in nested class case) second one will show error.

For more discussion on it go through that link

Using Application context everywhere?.

Hope it will help you


Android memory management. It covers all the aspects of Android memory management.

For context explanation this is a good answer.

Another good explanations of context.