Pages

Wednesday, February 10, 2016

parse.com current user returning null sometimes

A cool way of dealing with this problem i have found. In the activity, where current user is needed, i have a private field currentUser;


  1. Current user is set to a static helper class on login/registration or autologin
  2. Where needed in activity onCreate i get currentUser from my helper class by a getter.
  3. If after that currentUser is null, then i call the helper class initCurrentUser method, passing the activity instance as "this". 
  4. The method accepts anything that implements my custom UserCallback like this:public static <T extends CustomUserCallback> void initUser(final T t)
  5. In it i try to get user from parse by ParseUser.getCurrentUser(); If ok then return the value via callback's method setUser(user) that sets the currentUser in the calling activity.
  6. If parse returns null user, then i try to login the user with saved auth data in shared preferences, and return the user via callback.
  7. If even the login fails, that means, most likely, that password was changed, then app goes to the login screen.

And in the activity i dont initialize views in onCreate method. The method initializing views is called when activity has a valid currentUser, from onCreate or from the setUser from the callback.

No comments:

Post a Comment