Android: Keep username in session until logout

后端 未结 4 1557
感动是毒
感动是毒 2020-12-08 09:04

I\'m having hard time trying to figure out how to use SharedPreferences to store the username in the phone and stay in session until logout. I also need to know how at the s

4条回答
  •  没有蜡笔的小新
    2020-12-08 09:13

    When the user logs in successfully, add the user's username to the SharedPreference for your application.

    SharedPreferences settings = getSharedPreferences("MyPrefsFile", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("username", username);
    

    Every time the user opens the app, check to see if the "userID" preference is set. If it is then he is logged in. otherwise show the Login screen. When the user logs out, delete the "username" entry from the shared prefs.

    I also need to know how at the same time its in session it will send the username out with the data the user click on within the listview

    You will have access to the user's ID through that shared pref so you are able to use it whenever you need it. The Android framework does not do that for you but it's a simple task for you to do.

提交回复
热议问题