Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get my activity context?

I don't really get the idea behind how this whole thing works really, so if I have some class A that need the context of a class B which extends Activity, how do i get that context?

I'm searching for a more efficient way than giving the context as a parameter to class A constructor. For example if class A is going to have millions of instances then we would end up having millions of redundant pointer to Context while we should be able somehow to have just one somewhere and a getter function...

like image 559
Ofek Ron Avatar asked Sep 07 '12 15:09

Ofek Ron


People also ask

How do I find application context?

You can go for getApplicationContext() if you wanna get context of whole application. If you want to get context of current class you can use getBaseContext() instead.

How do I get context in kotlin fragment?

You can use the getActivity() method to get context or You can use getContext() method .


1 Answers

Ok, I will give a small example on how to do what you ask

public class ClassB extends Activity {   ClassA A1 = new ClassA(this); // for activity context   ClassA A2 = new ClassA(getApplicationContext());  // for application context.   } 
like image 92
Gan Avatar answered Sep 30 '22 09:09

Gan