global variables

Maintaining global Application state

SDK Version: 

As a possible solutions mentioned in previous article Leaving an Android application the Application object can come handy. If you want to store data, global variables that needs to be accessed from everywhere in the application, from multiple Activities, in other words is you want to maintain a global "state" of the whole application the Application object can help.

For this we must make a class which extends the class add our own methods to it, and define this class in the AndroidManifest.xml as below:

  1. An example for the Application class:
  3. public class HelloApplication extends Application {
  4.         private int globalVariable=1;
  6.         public int getGlobalVariable() {
  7.                 return globalVariable;
  8.         }

Syndicate content