The most practical way is to inherit the application, set the global variable inside, because application is the Android application portal, and the running cycle runs through the whole program.
Importandroid.app.Application; Public classMyApplicationextendsapplication{Private StaticContext Mcontext; Public StaticContext GetContext () {return This. Mcontext; } Public voidSetContext (Context mcontext) { This. mcontext=C; } @Override Public Static voidonCreate () {Mcontext= This; Super. OnCreate (); }}
Also add Android:name= "to the application property inside the Androidmanifest.xml. MyApplication ", indicating the application of his own definition
<application android:name= ". MyApplication " android:allowbackup=" true " android:icon=" @drawable/ic_launcher " android: Label= "@string/app_name" android:theme>
Then, in the whole program, you can call Myapplication.getcontext () directly when you need the context, and the other global variables are similar.
About setting global variables on Android get context at any time