Android, how to make it so user does not have to log in everytime app is reopened?

Solved!
Posted in General by Alexiz Hernandez Thu Jun 23 2016 07:51:17 GMT+0000 (UTC)·4·Viewed 1,279 times

I am trying to make user login and registration activities but I am having the issue where the user, after login or registration, does not stay logged in after app is closed. Here is the code that I have. CreateAccountActivity.java: private void createUser() { showProgressDialog(); String username = inputUsername.getText().toString().trim(); String email = inputEmail.getText().toString().trim(); String password = inputPassword.getText().toString(); Buddy.createUser(username, password, null, null, email, null, null, null, new BuddyCallback<User>(User.class) { @Override public void completed(BuddyResult<User> result) { if (result.getIsSuccess() && result.getResult() != null) { Intent intent = new Intent(CreateAccountActivity.this, MainActivity.class); startActivity(intent); finish(); } else { hideProgressDialog(); String errorMessage = result.getErrorMessage(); Toast.makeText(CreateAccountActivity.this, errorMessage, Toast.LENGTH_SHORT).show(); } } }); } LoginActivity.java: private void login() { showProgressDialog(); String username = inputUsername.getText().toString().trim(); String password = inputPassword.getText().toString(); Buddy.loginUser(username, password, new BuddyCallback<User>(User.class) { @Override public void completed(BuddyResult<User> result) { if (result.getIsSuccess() && result.getResult() != null) { Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } else { hideProgressDialog(); String errorMessage = result.getErrorMessage(); Toast.makeText(LoginActivity.this, errorMessage, Toast.LENGTH_SHORT).show(); } } }); } MainActivity.java: In my MainActivity, I check to see if there is a current user. The check seems to find the current user when I go to MainActivity from Login or Register because it doesn't kick me back to login. But, if I completely close the app and open again, there is not current user. private User user; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Buddy.init(getApplicationContext(), BackendSettings.APP_ID, BackendSettings.APP_KEY); showProgressDialog(); Buddy.getCurrentUser(new BuddyCallback<User>(User.class) { @Override public void completed(BuddyResult<User> result) { if (result.getIsSuccess() && result.getResult() != null) { user = result.getResult(); hideProgressDialog(); } else { Intent intent = new Intent(MainActivity.this, LoginActivity.class); startActivity(intent); finish(); } } }); } ALSO: When the user tried to log in or register, I would like to display custom messages if something goes wrong. I would like to use the result.getErrorCode() method. Is there a list of error codes somewhere that I can use?
Bradley Serbus
June 23, 2016

Hi Alexiz,

I don't see anything immediately amiss with your code. I'll try to duplicate your problem with the Android SDK. One thing to note from the SDK documentation, there's a callback you can implement that will get called when the user needs to log in. That will save you from having to check user login state yourself. Go to http://docs.buddy.com/docs/android-sdk and search for 'USER LOGIN/LOGOUT CALLBACK' for more details. I'll respond with error information to your other question on that topic.

Thanks,
Brad

Alexiz Hernandez
June 23, 2016

Hi Bradley,
Thank you for your response. I will look into your link. I look forward to hearing from you on the other topics. Thanks again!

Bradley Serbus
June 25, 2016

Hi Alexiz,

I've tracked down a compatibility issue in the Android SDK that can produce the behavior you are seeing. To resolve it, I've tightened up the naming on the file that stores the user token, to ensure that it can be read. I've released a new version of the SDK, 3.0.6. If you are using the '+' convention when specifying the SDK in your Gradle/Maven files ('com.buddy:androidsdk:3.0.0+'), you should pick it up automatically. Let me know if this resolves the issue for you. I'll get the error codes to you next.

Thanks,
Brad


Bradley Serbus marked this as solved
Markdown is allowed