First you have to declare global shared preferences:
SharedPreferences sp;
then in onCreate you delcare sp:
sp = getSharedPreferences("Session", 0); //"Session" is the file name it MUST be the same in all activities that require shared preferences.
So basically when the user login you check whether he is logged in, if it is true, then save his ID in the shared preferences using:
SharedPreferences.Editor e = sp.edit();
e.putInt("ID", ID-Value-that-you-used-it-to-check-for-ID);
e.commit();
and in the login class in onCreate, you add the second block to check whether the user is logged in and he has never logged out. and simply if he is then take him to the next activity.
if (cID != 0)
{
Intent iLogged = new Intent(this, The-Next-Class.class);
finish();
startActivity(iLogged);
}
whenever you want to retrieve the value you just add...
int ID = sp.getInt("ID", 0);
and to log out the user and delete the session use the following:
sp.edit().clear().commit();
Intent iLogout = new Intent(this, Login.class);
finish();
startActivity(iLogout);
break;
Edit 2:
What you got to do is...
1) retrieve the ID from the shared preferences.. now if the UserID column is int then use.
int ID = sp.getInt("ID", 0); // 0 is default value which means if couldn't find the value in the shared preferences file give it a value of 0.
2) you have to check, if the ID != 0 then pass the value to the query...
SELECT user_id, username, password
FROM users
WHERE user_id = ID