How to keep user logged in with facebook login?

Hello everybody,

I used the code generation tool to generate a facebook login interface for my android app. I would like to keep the user logged in as to skip the login activity at the next launch of the app, and jump directly to the main activity.
I read in the documentation that there’s a “boolean stayLoggedIn” attribute to the backendless API code, but it is not possible for me to add it with the generated code, it’s not exactly the same format.

Here is the code from the documentation:

public void Backendless.UserService.loginWithFacebook( android.app.Activity context, android.webkit.WebView webView, Map<String, String> facebookFieldsMappings, List<String> permissions, AsyncCallback<BackendlessUser> responder, boolean stayLoggedIn )

And here’s the generated code from my app:

package com.backendless.testbackendless.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.backendless.Backendless;
import com.backendless.BackendlessUser;

public class LoginActivity extends Activity
{
private Button facebookButton;

@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.login );

initUI();

Backendless.setUrl( Defaults.SERVER_URL );
Backendless.initApp( this, Defaults.APPLICATION_ID, Defaults.SECRET_KEY, Defaults.VERSION );

Backendless.UserService.isValidLogin( new DefaultCallback&lt;Boolean&gt;( 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&lt;BackendlessUser&gt;( 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 );
  }
});

}

private void initUI()
{
facebookButton = (Button) findViewById( R.id.loginFacebookButton );

facebookButton.setOnClickListener( new View.OnClickListener()
{
  @Override
  public void onClick( View view )
  {
    onLoginWithFacebookButtonClicked();
  }
} );

}

public void onLoginWithFacebookButtonClicked()
{
Map<String, String> facebookFieldsMapping = new HashMap<>();
facebookFieldsMapping.put( “name”, “name” );
facebookFieldsMapping.put( “gender”, “gender” );
facebookFieldsMapping.put( “email”, “email” );

List&lt;String&gt; facebookPermissions = new ArrayList&lt;&gt;();
facebookPermissions.add( "email" );

Backendless.UserService.loginWithFacebook( LoginActivity.this, null, facebookFieldsMapping, facebookPermissions, new SocialCallback&lt;BackendlessUser&gt;( LoginActivity.this )
{
  @Override
  public void handleResponse( BackendlessUser backendlessUser )
  {
    startActivity( new Intent( getBaseContext(), LoginSuccessActivity.class ) );
    finish();
  }
} );

}
}
How could I alter it to keep the user logged in?

Pardon me for my lack of knowledge, I’m a newbie in programming (as a lot of people say).

Thanks in advance,

Aurélien.

Hello Aurélien

Did you try to add stayLoggedIn argument? I can’t see it in the code you’ve shown. Please review the documentation again. Pay attention to the ‘Description’ section and compare it with your code.

Regards Anton

Thank You for your answer, I found out where my mistake was. I was adding the stayLoggedIn argument in the wrong spot, the IDE was flashing red everywhere; (and I forgot to include it in the code above).

It’s all working now, and starting the next activity.

I can’t begin to describe how amazing Backendless is!

Cheers,

Aurélien.

Thanks! Have a good day

Regards Anton