How to display only one time Login and then after start application directly in android

后端 未结 6 2117
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 15:34

I am getting trouble in making only one time login... My aim is first user gets login screen.. If he is new user he will register and then login...from then when ever user s

相关标签:
6条回答
  • 2020-12-29 15:51

    You can visit my blog

    http://upadhyayjiteshandroid.blogspot.in/2013/01/android-working-with-shared-preferences.html

    hope you will get the answer and understanding clearly

    Boolean flag;
    
    SharedPreferences applicationpreferences = PreferenceManager
                .getDefaultSharedPreferences(MainActivity.this);
    
    SharedPreferences.Editor editor = applicationpreferences .edit();
    
    flag = applicationpreferences .getBoolean("flag", false);
    
    if (flag) {
    ///second time activity
    
    }else{
    //first time
    editor.putBoolean("flag", true);
    editor.commit();
    }
    
    0 讨论(0)
  • 2020-12-29 15:54

    1.for storing in stored preferences use this

     SharedPreferences.Editor editor = getSharedPreferences("DeviceToken",MODE_PRIVATE).edit();
                            editor.putString("DeviceTokenkey","ABABABABABABABB12345");
    editor.apply();
    

    2.for retrieving the same use

        SharedPreferences prefs = getSharedPreferences("DeviceToken", 
     MODE_PRIVATE);
        String deviceToken = prefs.getString("DeviceTokenkey", null);
    
    0 讨论(0)
  • 2020-12-29 15:55

    Check out the Session Management in Android which shows you the way how you can manage the login if user is already logged into the application or not. And switch the user accordingly.

    Hope this will help you.

    0 讨论(0)
  • 2020-12-29 15:56

    Use SharedPreferences. contains which tell an key is present or not in SharedPreferences. Change your code as:

       SharedPreferences pref;
       SharedPreferences.Editor editor;
       pref = getSharedPreferences("testapp", MODE_PRIVATE);
       editor = pref.edit();
       if(pref.contains("register"))
        {
             String getStatus=pref.getString("register", "nil");
              if(getStatus.equals("true")){
                 redirect to next activity
               }else{
                //first time
                 editor.putString("register","true");
                 editor.commit();
               ///  show registration page again
               }
        }
        else{ //first time
                 editor = pref.edit();
                 editor.putString("register","true");
                 editor.commit();
               ///  show registration page again
        }
    
    0 讨论(0)
  • 2020-12-29 15:58

    Implement your SharedPreferences this way:

    Boolean isFirstTime;
    
    SharedPreferences app_preferences = PreferenceManager
                .getDefaultSharedPreferences(Splash.this);
    
    SharedPreferences.Editor editor = app_preferences.edit();
    
    isFirstTime = app_preferences.getBoolean("isFirstTime", true);
    
    if (isFirstTime) {
    
    //implement your first time logic
    editor.putBoolean("isFirstTime", false);
    editor.commit();
    
    }else{
    //app open directly
    }
    
    0 讨论(0)
  • 2020-12-29 16:11

    check it here

    http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

    A very good example of session management in android app.

    0 讨论(0)
提交回复
热议问题