Thursday, 3 December 2015

Session Management in Android Example | Android User Session Management

Hello warriors today i am going to share with you a very important topic used in almost every application which have login screen and name of the topic is Session Management in android.


Once user logged in to your app they don't want to login again until they log out from application manually.  for maintain this login session we use Session Management in android by using Shared preference. I have created a simple architecture of Session management in below picture i hope this will help you to understand coding.


Now time to implement this so like every post i am going to make a module of steps and we will follow that.

  • Create a new project in Android Studio.
  • Create two activity Login and Splash.
  • Create a java class name it SessionManager and add two method to it one for set status and one for get status using Shared preferences.
  • Add logout button to MainActivity and save status 0 on logout button click.
  • Add Two EditText to Login Activity one for Email and one for password also add one login button and save status 1 one on button click of Login button.
  • Check status in Splash if status is 0 launch login screen else launch MainActivity.
So lets start with programming by following all steps written above.

Create a new project in Android Studio Go to File-->New-->New Project and name it SessionManagement like below picture.




Select an Empty Activity from Template and leave every thing as default and finish it like below picture.


Create two more activity Login and Splash. Right Click on app-->New-->Activity-->Empty Activity.

Name of one is Login Activity like below picture.


and second one is Splash activity like below picture.


Create a java class name it SessionManager and add two method to it one for set status and one for get status using Shared preferences.

Create a new java class under same package. Right click on main package -->New-->Java class and Name it SessionManager like below picture.


Now after creating this class add two methods to it one for set status and one for get status. Name of the methods are setPrefrences() and getPrefrences() .we save status using Shared preferences and we will call set status method from Login Activity and MainActivity and get status from Splash Activity.
 here is setPrefrences() method .

  //method to save status  
 public void setPreferences(Context context, String key, String value) {  
   
     SharedPreferences.Editor editor = context.getSharedPreferences("Androidwarriors", Context.MODE_PRIVATE).edit();  
     editor.putString(key, value);  
     editor.commit();  
   
   }  

and here is getPreferences() method.

    
 public String getPreferences(Context context, String key) {  
   
       SharedPreferences prefs = context.getSharedPreferences("Androidwarriors",     Context.MODE_PRIVATE);  
     String position = prefs.getString(key, "");  
     return position;  
   }  

So here is my complete SessionManager class simply copy and paste complete code to your SessionManager java class.

 Add logout button to MainActivity and save status 0 on logout button click.

Now we need to add a logout button to our activity_main.xml. I am also adding a Textview to show welcome Text here is my activity_main.xml.

Open your MainActivity.java. we will save status to 0 on logout on logout button  click for that First we need to make an object of SessionManager class and call setPreferences() method on button click. here is my complete code of MainActivity.java.

In MainActivity you can see finish() method onclick of logout button this is used to close our application on logout. I have also written some code in onBackPress() method. in this i am stopping user from going to Login Screen from MainActivity.

Add Two EditText to Login Activity one for Email and one for password also add one login button and save status 1 one on button click of Login button.

Open you activity_login.xml and add two EditText and a Button to it. one EditText is for Email and second is for Password. Button is login Button here is my activity_login.xml.

Open Login.java. In this we will save status 1 on sucsessfull login on click of  login button. I am also checking that EditText are not Empty before login. here is my complete Login.java.

Check status in Splash if status is 0 launch login screen else launch MainActivity.

Now moving towards last step Open activity_splash and add a ImageView and a TextView just for logo and name of application. here is my activity_spalsh.

Now Open your Splash.java in this I am using Thread to hold splash screen for some time you can use Handler also for this. we need to check status if status is 0 open MainActivity else open Login Activity. here is my Splash.java

Finally we have completed all the steps. I hope this was helpfull for you don't forget to give your feedback in comment.

http://www.mediafire.com/download/39au7s7iprs537v/SessionManagement.zip

0 comments:

Post a Comment