Monthly Archives: January 2012

Maintaining Global Application State through Android Application

Lots of time we need to maintain and share the Application state throughout the Application. We may need to share few strings or may be few global variables or few flags among activities and services and my be other application components.

In these scenarios the “Application” class will become handy.
The “Application” class can be extended and we can add the needed data members and methods to it, which help us to set and get the data across application components.

Following class is inherited from Application class, which holds an int and a string val.

class  MyApplication extends Application {
private int intData;
private String stringData;public void setIntData( int intData ) {
this.intData = intData;
}public void setStringData( String stringData ) {
this.stringData = stringData;
}

public int getIntData() {
return intData;
}

public String getStringData() {
return stringData;
}
}

Once you have got the Application class i.e. MyApplication class in this case, it has to be associated with your application. The “MyApplication” class is associated with your application through the “Android Manifest” file. Following snippet shows how to do it.

 <application
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name” android:name=”.MyApplication” >
….
….

When your application starts executing an object of “MyApplication” class is created implicitly. This is object is available and will exist in memory throughout the application (till the time at least an activity of your app is available).
We can obtain a reference to the “MyApplication” object using the getApplicationContext() or getApplication() method.

Setting and Getting Data using the Application object

Now as we have set up the MyApplication class with the application, it is time to see how could we use it.
To demonstrate this we will be using two Activities. We well set data from one activity and will read the data from another activity.

Activity 1: ( In this Activity we will set the data)

….
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);MyApplication myApplication = (MyApplicaton) getApplicationContext();
// set some data
myApplication.setIntData( 101 );
myApplication.setStringData(“BitCode”);
// the data is set now, so you start new act now
}
….

Activity 2: (In this Activity we will read the data set by Activity 1)

 ….
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);MyApplication myApplication = (MyApplicaton) getApplicationContext();
// get the data back
int intData = myApplication.getIntData();
String stringData = myApplication.getStringData();
// do something with data as per your business logic
…..
}
….

This is how you can use the Application class and link it with your application to maintain and share the Global Application State in an Android Application.

BitCode Blog

Hi All,

We are happy to introduce our blog at http://bitcode.in/blog.
We hope that this blog will help you find most of the technical stuff you need everyday. Also we promise to put lots of innovative stuff here on our blog.

Thank You.
Admin BitCode