I'm creating an android application that signs in with facebook. This is already working, but something is happening that I can not solve.
According to the examples of the facebook documentation itself, the code that makes the calls to facebook api is inside the onCreate()
method, and this causes me to open the login page (which has the login button with facebook) it already automatically opens facebook to ask for the permissions even without I have clicked the login button with facebook.
What do I do so that it will only open the login screen when I click the "Log in with Facebook" button?
Here are 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>