19 November 2014

How to access global variable in android all activities

Hi in this post i will describe how to use global variable to access all the activities in android application.First you have to define application class in manifest file.


<application android:label="@string/app_name" 

     android:icon="@drawable/logo"

     android:name="com.homesystem.net.CustomApplication">

</application>

CustomApplication class is a class which used to store and access global variables.

public class CustomApplication extends Application {

    public static String message = "";

    public static boolean state =true;

    private static CustomApplication singleton;

    public static CustomApplication getInstance() {

        return singleton;

    }

    @Override
    public void onCreate() {
        super.onCreate();
        singleton = this;
    }


    }


And then inside any activity you can update and real values in CustomApplication class.

String Message = CustomApplication .message;
boolean state=CustomApplication .state;


No comments:

Post a Comment