Click here to Skip to main content
13,298,175 members (76,849 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 26 Jun 2013

Global Variable in Android

, 26 Jun 2013
Rate this:
Please Sign up or sign in to vote.
This tip will help you to develop global variable in Android application.


When we need to have several variables across applications, we can go for global variable. Here, I am going to explain, defining global variable by extending the Android's Application class. This is the base class for maintaining global application state.

Steps for Declaring Global Variable

  1. Create a class that extends 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;
  2. Add the class to the AndroidManifest file as an attribute of <application> tag:
            .... />  
  3. You can access it from any context using the Context.getApplicationContext() method. Now, we can access the global data across the application like:
    Global global;
       public void onCreate(Bundle savedInstanceState) {
           Boolean notification=global.get_notification();}


  • 26th June, 2013: Initial post

This tip is based on the following blog:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India
No Biography provided

You may also be interested in...


Comments and Discussions

General我的做法: Pin
wu.qingman10-Jul-13 16:21
memberwu.qingman10-Jul-13 16:21 
GeneralMy vote of 4 Pin
Swapnil Mahadev Sonar8-Jul-13 21:24
memberSwapnil Mahadev Sonar8-Jul-13 21:24 
QuestionYou should use static members Pin
va_dev2-Jul-13 6:11
memberva_dev2-Jul-13 6:11 
QuestionType casting is bad idea Pin
Izbyakov26-Jun-13 5:08
memberIzbyakov26-Jun-13 5:08 
GeneralMy vote of 1 Pin
Izbyakov26-Jun-13 5:06
memberIzbyakov26-Jun-13 5:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 26 Jun 2013
Article Copyright 2013 by josh-jw
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid