Current user returns Null || Android

Hello … When the user opens my app … it checks if there is user logged in or not … So I use that code to check:

if ( Backendless.UserService.CurrentUser() == null){
startActivity(new Intent(MainActivity.this, LogIn.class));
}

So when I login to the app … then close it and remove it from " the last opened app " in the emulator …then I reopen the app … current user returns null even if the user haven’t logout …and the Login Activity open why is that happen ? that mean CurrentUser method data will be deleted if I removed the app from “last opened app” ? Note : I mean remove the app from " last opened app " Not remove the app data from task manager or setting …And Thanks

Hi,

The reason is that we cannot keep the whole user object - it may be too large, so we just keep his objectId. This means that you should retrieve the user by objectId when he opens the application (in case you had chosen to keep user logged in when logging in).

Here is an example from our code generation (by the way, you can download it for your app, too):

Backendless.UserService.isValidLogin( new DefaultCallback<Boolean>( this )
{
 @Override
 public void handleResponse( Boolean isValidLogin )
 {
 if( isValidLogin && Backendless.UserService.CurrentUser() == null )
 {
 String currentUserId = Backendless.UserService.loggedInUser();

 if( !currentUserId.equals( "" ) )
 {
 Backendless.UserService.findById( currentUserId, new DefaultCallback<BackendlessUser>( LoginActivity.this, "Logging in..." )
 {
 @Override
 public void handleResponse( BackendlessUser currentUser )
 {
 super.handleResponse( currentUser );
 Backendless.UserService.setCurrentUser( currentUser );
 startActivity( new Intent( getBaseContext(), LoginSuccessActivity.class ) );
 finish();
 }
 } );
 }
 }

 super.handleResponse( isValidLogin );
 }
});

So that mean if there is user already logged in the second method will be called and if there is no user the first one will be called ?

This means that if user is already logged in, which you can check using:

Backendless.UserService.isValidLogin( asyncCallback )

then you can retrieve his objectId:

String currentUserId = Backendless.UserService.loggedInUser();

and then retrieve the user itself:

Backendless.UserService.findById( currentUserId, asyncCallback )

and set him to CurrentUser:

Backendless.UserService.setCurrentUser( currentUser );

Ok thanks …then how can I get the user properties ? I want to get them outside the method

You receive BackendlessUser object in response to Backendless.UserService.findById( currentUserId, asyncCallback ). You can get any user’s property from BackendlessUser object using its getProperty() method.

Thank you … I will try that

it is not working

Could you please provide your Activity code?

:frowning: I tried it and it’sn’t woring

ok wait please

I make a method and named it userLogged()

and this is the code inside this method :
Backendless.UserService.isValidLogin(new AsyncCallback<Boolean>() {
@Override
public void handleResponse(Boolean loggedIn) {
if(loggedIn && Backendless.UserService.CurrentUser() == null){
String CurrentUserId = Backendless.UserService.loggedInUser();
if(!CurrentUserId.equals("")){
Backendless.UserService.findById(CurrentUserId, new AsyncCallback<BackendlessUser>() {
@Override
public void handleResponse(BackendlessUser curren User) {
Backendless.UserService.setCurrentUser(currentUser);
}
@Override
public void handleFault(BackendlessFault backendlessFault) {}
});
}
}else{}
}
@Override
public void handleFault(BackendlessFault backendlessFault) {}
}); then after i call this method …I check for current user and it’s returns null : userLoggedIn();
if ( Backendless.UserService.CurrentUser() == null){
}

Have you checked what value you receive into loggedIn variable?

no I haven’t

Please debug your code and see on which step it fails to retrieve current user.

now I checked that by A Toast message … the error is that loggedIn is false

Do you set stayLoggedIn to true when logging in for the first time?

what is stayLoggedIn ?

It is the argument for login method which tells us to keep user’s objectId in order for you to just retrieve it later instead of logging in again.

See the docs: http://backendless.com/documentation/users/android/users_login.htm

yeah now I set it to true but still not working