Its very difficult to maintain Login Session in android till the time you read this blog. Once you have gone through this blog all doubts will be cleared.
String KEY_LOGIN_NAME = "LoginStatus";
SharedPreferences preferences;
------------------------------------------------------------------------
//Initialise sharedPreferences Object
preferences = getSharedPreferences("PREF_NAME", MODE_PRIVATE);
Intent intent ;
------------------------------------------------------------------------
// Check for login status Using SharedPreference
if (!isLogin()) {
Log.d("Splash Activity ", "User not logged in so Login Screen will appear");
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
else {
Log.d("Splash Activity ", "User is logged in so Home Screen will appear");
intent = new Intent(SplashActivity.this, HomeScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
------------------------------------------------------------------------
// Method to check Login Status
private boolean isLogin() {
return preferences.getBoolean(KEY_LOGIN_NAME, false);
}
For Logout Operation :
We can Use Shared preferences For Login session, Once the user logs in to your application, you can check its authenticity and once the user logged in to application you can store state of Login for that user.
Steps for User Login Session:
1. Store Login status as true in SharedPreference for authenticate user.
2. Check On Splash(First) Activity for Login Status and depending on that load next Activity
3. On Logout button set Login status False
1. Store Login status as true in SharedPreference for authenticate user.
2. Check On Splash(First) Activity for Login Status and depending on that load next Activity
3. On Logout button set Login status False
Here is an sample code snippet for above operation,
Code Snippet SplashActivity
SplashActivity class
String KEY_LOGIN_NAME = "LoginStatus";
SharedPreferences preferences;
------------------------------------------------------------------------
//Initialise sharedPreferences Object
preferences = getSharedPreferences("PREF_NAME", MODE_PRIVATE);
Intent intent ;
------------------------------------------------------------------------
// Check for login status Using SharedPreference
if (!isLogin()) {
Log.d("Splash Activity ", "User not logged in so Login Screen will appear");
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
else {
Log.d("Splash Activity ", "User is logged in so Home Screen will appear");
intent = new Intent(SplashActivity.this, HomeScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
------------------------------------------------------------------------
// Method to check Login Status
private boolean isLogin() {
return preferences.getBoolean(KEY_LOGIN_NAME, false);
}
code snippet LoginActivity
LoginActivity.class
SharedPreferences preferences;
SharedPreferences.Editor editor;
String KEY_LOGIN_NAME = "LoginStatus";
---------------------------------------------------------------------------
preferences = getSharedPreferences("PREF_NAME", MODE_PRIVATE);
editor = preferences.edit();
btnLoginUser.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// Check for valid Username and Password
if(valid)
{
// Valid user
editor.putBoolean(KEY_LOGIN_NAME, true);
editor.commit();
}
else
{
// Enter valid Username or Password
}
}
});
SharedPreferences.Editor editor;
String KEY_LOGIN_NAME = "LoginStatus";
---------------------------------------------------------------------------
preferences = getSharedPreferences("PREF_NAME", MODE_PRIVATE);
editor = preferences.edit();
btnLoginUser.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// Check for valid Username and Password
if(valid)
{
// Valid user
editor.putBoolean(KEY_LOGIN_NAME, true);
editor.commit();
}
else
{
// Enter valid Username or Password
}
}
});
Code Snippet
For Logout Operation :
btnLoginUser.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Clear from SharedPreference
editor.putBoolean(KEY_LOGIN_NAME,false);
editor.commit();
finish();
}
});
Thanks,
Hopefully this post will Help You…
Happy Coding
Visit Us- Stark Digital Media Services Pvt Ltd,
Shinde Complex, Near Maratha Mandir, Shinde Nagar, Bavdhan,Pune, Maharashtra 411021.
@Override
public void onClick(View v) {
// Clear from SharedPreference
editor.putBoolean(KEY_LOGIN_NAME,false);
editor.commit();
finish();
}
});
Thanks,
Hopefully this post will Help You…
Happy Coding
Visit Us- Stark Digital Media Services Pvt Ltd,
Shinde Complex, Near Maratha Mandir, Shinde Nagar, Bavdhan,Pune, Maharashtra 411021.
020 – 22952445
Our Best Services- Android App development, iOS App Development, Web Development & Design Services etc,