Reputation: 75
I am currently developing a simple android application with login connected to a database. (mysql database). However, I want to provide session which will only be terminated when the user logs out in the app. How to do it?
Upvotes: 0
Views: 60
Reputation: 482
You can put the email or userid in the sharedpreference once the user successfully logged in. The sharedpreference values remain even if you exit the application or switch off the mobile, next time when the user visits the app, you can check whether the sharedpreference variable you declared contain the value. if yes, redirect to the page you want else redirect to login page. When the user logouts, delete the sharedpreference variable.
This is when user successfully log in
public static String filename = "MySharedString";
SharedPreferences someData;
someData = getSharedPreferences(filename, 0);
SharedPreferences.Editor editor = someData.edit();
editor.putString("myemail", email);
editor.commit();
Now to check next time (probably in splash activity)
if (someData.contains("myemail"))
{
s = Integer.parseInt((someData.getString("myemail", "")));
// to the page after login
}
else
{
redirect to the login page
}
When user clicks logout button
SharedPreferences.Editor editor = someData.edit();
editor.clear();
editor.commit();
Hope it helps..
Upvotes: 1