How to get your activity context?

I really don't understand how it all works, so if I have a class A that needs a context of class B that extends Activity , how can I get this context

I'm looking for a more efficient way that gives context as an argument to the class A constructor, since for exsmple, if class A has millions of instances, then we get millions of redundant pointer to Context , while we should somehow have only one and a function getter ...

+45
android android-context
Sep 07 2018-12-12T00:
source share
6 answers

You can use the Application class (an open class in the android.application package), that is:

The base class for those who need to maintain the global state of the application. You can provide your own implementation by specifying its name in your AndroidManifest.xml tag, which will call this class for you when the process is for your application / package.

To use this class, follow these steps:

 public class App extends Application { private static Context mContext; public static Context getContext() { return mContext; } public static void setContext(Context mContext) { this.mContext = mContext; } ... } 

In your manifest:

 <application android:icon="..." android:label="..." android:name="com.example.yourmainpackagename.App" > class that extends Application ^^^ 

In action B:

 public class B extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sampleactivitylayout); App.setContext(this); ... } ... } 

In class A:

 Context c = App.getContext(); 

Note :

Typically, there is no need for a subclass of Application. In most cases, static singlets can provide the same functionality in a more modular way. If your singleton needs a global context (for example, to register broadcast receivers), a function to get it can be provided with a Context that internally uses Context.getApplicationContext () when we first build a singleton.

+28
Sep 07 '12 at 15:45
source share

OK, I will give a small example of 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. } 
+40
Sep 07
source share

The best and easiest way to get an activity context is to place .this after the name Activity. For example: if your activity name is SecondActivity , its context will be SecondActivity.this

+19
Oct 22 '16 at 15:09
source share

you pass the context to class B in its constructor and make sure you pass getApplicationContext () instead of ActivityContext ()

+2
Sep 07 '12 at 15:13
source share

You can create a constructor using the Context parameter of class A, then you can use this context.

Context c;

A (contextual context) {this.c = context}

From operation B, you create an object of class A using this constructor and pass getApplicationContext ().

+1
Sep 07 '12 at 15:25
source share

If you need context A in B, you need to pass it to B, and you can do this by passing the Activity A parameter as another. I don’t see a big problem with the fact that many instances of A have their own pointers to B, I’m not sure that this will be even most of the overhead.

But if this is a problem, it is possible to save a pointer to A as a kind of global, avariable Application class, as suggested by @hasanghaforian. In fact, depending on what you need the context for, you might even use the Application context.

I would suggest reading this article about context in order to better understand what context you need.

+1
09 Oct '13 at 11:31 on
source share



All Articles