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.

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.
SharedPreferences myprefs = getApplicationContext()
                                  .getSharedPreferences("user", MODE_PRIVATE);
                                  .putString("userid", userid)

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
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",
              SharedPreferences.Editor editor = myprefs.edit();

Then move the user to login Activity or complete exit the user by using below code..
Intent i =new Intent(Intent.ACTION_MAIN);

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


Post a Comment