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...
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.
You can use the getActivity() method to get context or You can use getContext() method .
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. }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With