I am managing the session using shared preference in my application. If the user is logged in it must show the home activity, if not it must show login activity.
With the help of http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
I tried to create home and redirect to login activity if the user is not logged in.
Is this right?, or is there any better solution.
Thanks, Bennet.
Here is what Im doing when logging the user in :
private static final String APP_SHARED_PREFS = "asdasd_preferences";
SharedPreferences sharedPrefs;
Editor editor;
private boolean isUserLoggedIn;
sharedPrefs = getApplicationContext().getSharedPreferences(APP_SHARED_PREFS, Context.MODE_PRIVATE);
isUserLoggedIn = sharedPrefs.getBoolean("userLoggedInState", false);
editor = sharedPrefs.edit();
editor.putBoolean("userLoggedInState", true);
editor.putInt("currentLoggedInUserId", userId);
editor.commit();
Intent signupSuccessHome = new Intent(this, Home.class);
signupSuccessHome.putExtra("reqFrom", "login");
startActivity(signupSuccessHome);
finish();
in the base activity that all my activities extend (it contains the actionbar and the sliding menu) I have the following check: (mainactivity is my login/register screen. if the user is not logged in im sending them there)
@Override
protected void onResume() {
sharedPrefs = getApplicationContext().getSharedPreferences(APP_SHARED_PREFS, Context.MODE_PRIVATE);
isUserLoggedIn = sharedPrefs.getBoolean("userLoggedInState", false);
if (!isUserLoggedIn) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
super.onResume();
}
@Override
protected void onRestart() {
sharedPrefs = getApplicationContext().getSharedPreferences(APP_SHARED_PREFS, Context.MODE_PRIVATE);
isUserLoggedIn = sharedPrefs.getBoolean("userLoggedInState", false);
if (!isUserLoggedIn) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
super.onRestart();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPrefs = getApplicationContext().getSharedPreferences(APP_SHARED_PREFS, Context.MODE_PRIVATE);
isUserLoggedIn = sharedPrefs.getBoolean("userLoggedInState", false);
currentlyLoggedInUser = sharedPrefs.getInt("currentLoggedInUserId", 0);
currentlyLoggedInUserString = Integer.toString(currentlyLoggedInUser);
if (!isUserLoggedIn) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
and to prevent the user from going back to the login screen: in Login.java:
isUserLoggedIn = sharedPrefs.getBoolean("userLoggedInState", false);
if (isUserLoggedIn) {
Intent intent = new Intent(this, Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
in Home.java:
@Override
public void onBackPressed() {
Intent intent = new Intent(this, Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
super.onBackPressed();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With