Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Login Activity and Home Activity redirection

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.

like image 581
Bennet Avatar asked Jul 29 '13 02:07

Bennet


1 Answers

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();
}
like image 139
Kaloyan Roussev Avatar answered Sep 24 '22 13:09

Kaloyan Roussev