Android Application Global class

Hi in this blog i am going to write about the application global class.
You can provide your own implementation by specifying its name in your AndroidManifest.xml's <application> tag.
Below is the code tutorial of application global class:
Step1:) Create a new class which extends the application class as follows:


public class ApplicationSetterClass extends Application{
    private String mname;
    private String mage;
    // This Method will get the name  saved in application class
    public String getName()
    {
        return mname;
    }

    // This Method will set the name in application class
    public void setName(String mname)
    {
        this.mname=mname;
    }

    // This Method will get the age  saved in application class
    public String getAge()
    {
        return mage;
    }

    public void setAge(String mage)
    {
        this.mage=mage;
    }
}

Step2):Now  declare your application gobal class in application tag in android  manifest file as follows:

<application android:icon="@drawable/icon" android:name="ApplicationSetterClass" android:label="@string/app_name">
</application>


Step3)Now your applciation global class is ready to save the application state:Make a object of the ApplicationSetterClass in your activty where you want to use it as follows:

ApplicationSetterClass  appset=(ApplicationSetterClass) getApplication(); 


Now you can save any data as follows:

 appset.setName("Your droid tutorial");
 appset.setAge("21");


And now this data will be available  globally to whole application:
You can retrieve data as follows:


appset.getName();
appset.getAge();


Source code:https://sites.google.com/site/quickandroidblog/download-stuff

Comments

Post a Comment

Popular Posts