vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Saturday, September 1, 2012

How to use Login and Logout process in android application


Here I would like to share my ideas .How to use login and Logout process in android application.

Website:
Normally when using website we used to store Username and password in session, whenever we want the user details we can easily get from session.
When Logout we simple clear the session and redirect the user to login page.

Android Application:

In android application we can follow the same methods like website. But instead of using session here in android we use ‘ShredPrefrence ‘.SharedPrefrence is like session, we can easily store and get the information from the sharedPrefrence. Also we can easily remove all the details in shared preference.
In Previous article I discuss about store the usage of SharedPrefrence. For your information I provide the link as below.

Steps 1: Login
When user submit the username and password just validate the login details, then store the details in sharedprefrence.
Code:
SharedPreferences myprefs = getApplicationContext()
                                  .getSharedPreferences("user", MODE_PRIVATE);
                     myprefs.edit()
                                  .putString("userid", userid)
                                  .commit();

If you want to check the stored details in sharedprefrence you can use the below code.
SharedPreferences myprefs = getApplicationContext();
                     boolean username_set = myprefs.contains("userid ");
                     if (username_set ) {
                           return true;
                     } else {
                           return false;
                     }

It will check the userid is present in the sharedprefrence if its not it will return as false
LogOut:
In Logout button, when the user click logout you need to clear the stored details in Sharedprefrence,then move to login page .
In such case you just use the below code, when doing Logout Process.
SharedPreferences myprefs = c.getSharedPreferences("user",
                           Context.MODE_PRIVATE);
              SharedPreferences.Editor editor = myprefs.edit();
              editor.remove("userid");

Then move the user to login Activity or complete exit the user by using below code..
Intent i =new Intent(Intent.ACTION_MAIN);
              i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
              i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              i.addCategory(Intent.CATEGORY_HOME);
c.startActivity(i);

Thas’it I mostly I cover all the details that have been used in Login process should follow in android
application.
If you have any doubts regarding the process just post your comments below..
Happy Coding……..

0 comments:

Post a Comment