When we need to define global variables in the entire Application, we can extend the Android Application class. Here is a basic class used to operate the global Application status.
To create a global variable, follow these steps:
1) create a new class to expand from the Application class:
public class Global extends Application { private Boolean _notification=false; public Boolean get_notification() { return _notification; } public void set_notification(Boolean _notification) { this._notification = _notification; } } public class Global extends Application { private Boolean _notification=false; public Boolean get_notification() { return _notification; } public void set_notification(Boolean _notification) { this._notification = _notification; }
} 2) Add a new class to the AndroidManifest file as the property of the application Tag:
<application android:name=".Global" .... /> <applicationandroid:name=".Global" .... />3)
You can use Context. getApplicationContext () to access the global variable:
Global global; public void onCreate(Bundle savedInstanceState) { global=((Global)getApplicationContext()); Boolean notification=global.get_notification();} Global global; public void onCreate(Bundle savedInstanceState) { global=((Global)getApplicationContext()); Boolean notification=global.get_notification();}