Android Session Management Tutorial

Email this to someonePrint this pageShare on Google+0Share on Facebook11Tweet about this on TwitterShare on LinkedIn1Share on Reddit0Pin on Pinterest0Share on StumbleUpon1

Android Session Management

Creating Android Session or login page fuctionality and save user data as like in session. After loging user will redirect to login screen and after back button click do not show login page. With shared preferences to save and get user data globally through out the application

Android Session Management Tutorial

Ways to save data globally

1. Global variables 

2. shared preferences

1-Global variables

Defining variable in application context and you can use this variable as a Global variable. You can  set value in this variable and get value on any activity / broadcast receiver / service in application context.
In further examples you will see  use to follow SINGLETON pattern and MVC pattern in android.
The problem is to save value across several Activities and all parts of your application context. You can achieve this by creating static variable but it is not the good way it influencing towards memory leaks. The way in Android is to associate your variable with the Application context.
Every Application has a context, and Android guarantees that Application context will exist as a instance across your application.The way to do this is to create a class and extends with android.app.Application, and specify your class in the application tag in your AndroidManifest.xml file. Android will create an instance of that class and make it available for your entire application context. You can get object of your class on any activity / broadcast reciever / service in application context(environment) by Context.getApplicationContext() method.

WorkFlow :

variable_workflow

SRC/GlobalClass.java

Create your custom class subclass of android.app.Application class. you will use this class as global class for your Application environment(Conext).

AndroidManifest.xml

Assign GlobalClass.java in application tag, see this line in application tagandroid:name=”com.androidexample.globalvariable.GlobalClass”

After assign you can get GlobalClass.java instance on any activity / broadcast reciever / service in application context.

SRC/FirstScreen.java

getApplicationContext() method of Context will give GlobalClass.java instance and set name/email values and maintain state of GlobalClass.java instance.

SRC/SecondScreen.java

Call getApplicationContext() method of Context and get GlobalClass.java instance , By GlobalClass.java instance you can get set values for name/email variables.

SRC/ThirdScreen.java

Call getApplicationContext() method of Context and get GlobalClass.java instance , By GlobalClass.java instance you can get set values for name/email variables.

2-shared preference:

You can save and retrieve key, value pair data from Shared preferences.
SharedPreferences values will persist across user sessions. Data in shared preferences will be persistent even though user closes the application.
You can get values from Shared preferences using getSharedPreferences() method.
You also need an editor to edit and save the changes in shared preferences.
Use SharedPreferences to store data: booleans, floats, ints, longs, and strings.

Available mode for shared preference:

 1. MODE_WORLD_READABLE
 2. MODE_WORLD_WRITEABLE
 3. MODE_PRIVATE

Store,Fetch,Remove,Clear Data from SharedPreferences.

Now  look at your file structure

UserSessionManager.java

This class contain all user session related functions.creating SharedPreferences and inserting / updating / deleting user session data from SharedPreferences.further explanation given as comment in class code.

LoginActivity.java

This is the first screen( Login screen ).
For Login using Username: admin and Password: admin
Using createUserLoginSession() to save user data in SharedPreferences for further use.

activity_login.xml

This file is used as layout file in LoginActivity.java.It will create login screen design.

MainActivity.java

This is the welcome screen after user login.Get user data from SharedPreferences and show on activity.

activity_login.xml

This file is used as layout file in MainActivity.java.It will create Welcome screen (after login) design.

Email this to someonePrint this pageShare on Google+0Share on Facebook11Tweet about this on TwitterShare on LinkedIn1Share on Reddit0Pin on Pinterest0Share on StumbleUpon1

About Team

Browse Archived Articles by Team

  • android

    hi,

    friends i am learn with Android Session Management to the android example.com