Login facebook opening alone on android app

Asked

Viewed 401 times

2

I am creating an android application that login with facebook. This is already working, but something is happening that I am not able to solve.

According to the examples of the facebook documentation itself, the code that makes the calls to the facebook api is within the method onCreate(), and this makes it as soon as I open the login page (which has the login button with facebook) it already automatically opens facebook to ask for permissions even without me having clicked the login button with facebook.

What I do to make it only open the login screen when I click on "Log in with Facebook"?

Follow the codes:

Loginactivity.java

public class LoginActivity extends Activity {

CallbackManager callbackManager;
LoginButton loginButton;
LoginManager loginManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    Log.d("Teste", "onCreate inicio");

    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();

    loginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // App code
            Log.d("Teste", "onSuccess inicio [Token = " + loginResult.getAccessToken() + "], [Permissões = " + loginResult.getRecentlyGrantedPermissions() +
                    "], [Negações = " + loginResult.getRecentlyDeniedPermissions() + "] Json: "+ loginResult.toString());
        }

        @Override
        public void onCancel() {
            // App code
            Log.d("Teste", "onCancel inicio");
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
            Log.d("Teste", "onError( " + exception.toString() + ")");
        }
    });


}

activity_login.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F76B03"
android:scrollbars="none"
android:gravity="center">

<com.facebook.login.widget.LoginButton
    android:id="@+id/login_button"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="5dp"
    android:layout_gravity="center_horizontal"
    android:text="ou"
    android:textColor="#000000" />


<EditText style="@style/tvFullParent"
    android:id="@+id/etLogin"
    android:background="@drawable/shapeedittext"
    android:inputType="textEmailAddress"
    android:drawableLeft="@drawable/ic_action_user"
    android:hint="E-mail"/>

<EditText style="@style/tvFullParent"
    android:id="@+id/etPassword"
    android:background="@drawable/shapeedittext"
    android:inputType="textPassword"
    android:drawableLeft="@drawable/ic_action_lock_closed"
    android:hint="Senha"/>

<Button
    android:id="@+id/btnLogin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="22dp"
    android:layout_marginRight="22dp"
    android:text="@string/btnLogin"/>

androidmanifest.xml

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />

    <activity
        android:name=".SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".LoginActivity"
        android:label="@string/title_activity_login"
        android:screenOrientation="portrait">
    </activity>
    <activity android:name="com.facebook.FacebookActivity"
        android:configChanges=
            "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:label="@string/app_name" />
    </application>

1 answer

1

From what I understand of documentation, in the onCreate you create and perform all the records just as you have done, but the method logInWithReadPermissions is the call to the action that actually performs the authentication, however, only when you do not use the LoginButton and yes some custom button, so you put this method in the OnClickListener.

This is what it says in this passage, just before the indication of this method:

Then you can later perform the current login, such as in a custom button’s Onclicklistener:

As is not your case, since you are using the LoginButton, you can delete this line and let the button itself execute its authentication method while being pressed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.