Search This Blog

Thursday, March 8, 2012

Maintaining the state of the User in Android application throughout

you can use the Application class in you activity. By extending the Application class you can maintain the stat in android throughout the application. It means you can call those variables in the application in all the activity.

public class SampleApplication extends Application {

private static String username;
private static String password;

public void onCreate() {

public static String getUsername() {
return username;

public static void setUsername(String username) {
SampleApplication.username = username;

public static String getPassword() {
return password;

public static void setPassword(String password) {
SampleApplication.password = password;


By using this code you can set and get the username and password in any activity. And maintain the state of your username and password. Just you need to add the class name in the manifest.xml like this way.

android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SampleApp"
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

You can access you application variables with static getters and setters from any activity or service.:

String currentUserName=SampleApplication.getUsername();
String currentPassword=SampleApplication.getPassword();

By this you can maintain the stat of the user in all over the application.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.