Wednesday, 3 July 2013

Global object / variable in Andoird Application


When we need to define global variables or global object in Android, we can extend the base android.app.Application class. Here are the details step: (1) Define a class extending the android.app.Application class
public   class   GlobalAccess  extends  Application {  
    private  Boolean notification= false ;  
    public  Boolean getNotification() {  
        return  notification;  
    }  
    public  void  setNotification(Boolean notification) {  
        this.notification = notification;  
    }  
}  
(2) Add to AndroidManifest.xml
< application  
android:name = ".GlobalAccess"  
        ....  /> 
(3) Now, we can use Context.getApplicationContext() method to get this Global object / variable.
GlobalAccess global;  
public  void  onCreate(Bundle savedInstanceState) {  
    global=((GlobalAccess)getApplicationContext());  
    Boolean notification=global.getNotification();
}

0 comments:

Post a Comment