Advertisement
Guest User

Untitled

a guest
Mar 4th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. PreviousActivity.userobj
  2.  
  3. public class UserCreator
  4. {
  5. public static User getUser(Context context)
  6. {
  7. SharedPreferences prefs = context.getSharedPreferences("Name", Context.MODE_PRIVATE);
  8.  
  9. //Check if the user is already stored, if is, then simply get the data from
  10. //your SharedPreference object.
  11.  
  12. boolean isValid = prefs.getBoolean("valid", false);
  13.  
  14. if(isValid)
  15. {
  16. String userName = prefs.getString("username", "");
  17. String passWord = prefs.getString("password", "");
  18. ...
  19. return new User(userName, passWord,...);
  20. }
  21. //If not, then store data
  22. else
  23. {
  24. //for example show a dialog here, where the user can log in.
  25. //when you have the data, then:
  26.  
  27. if(...login successful...)
  28. {
  29. SharedPreferences.Editor editor = prefs.edit();
  30. editor.putString("username", "someusername");
  31. editor.putString("password", "somepassword");
  32. editor.putBoolean("valid", true);
  33. ...
  34. editor.commit();
  35. }
  36. // Now, if the login was successful, then you can recall this function,
  37. // and it will return a valid user object.
  38. // if it was not, then it will show the login-dialog again.
  39. return getUser(context);
  40. }
  41. }
  42. }
  43.  
  44. User user = UserCreator.getUser(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement