1

I am building an android app with the facebook idk for login. I have followed their getting started guide and all worked but when i press the button the screen flashes and returns to the login activity. This is my LoginActivity:

protected void onCreate(Bundle savedInstanceState) {
    FacebookSdk.sdkInitialize(getApplicationContext());

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions("user_friends");


    // Callback registration
    CallbackManager callbackManager =                            CallbackManager.Factory.create();
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()

            {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Log.d(TAG_FACEBOOK, "Success!");
                    Log.d(TAG_FACEBOOK, loginResult.toString());
                }

                @Override
                public void onCancel() {
                    Log.d(TAG_FACEBOOK, "Cancel");
                }

                @Override
                public void onError(FacebookException exception) {
                    Log.d(TAG_FACEBOOK, "Error");
                }
            }

    );

And my xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".LoginActivity">

<TextView android:text="Hello World!" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<com.facebook.login.widget.LoginButton
    android:id="@+id/login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    android:layout_marginBottom="30dp" />


</RelativeLayout>

Can someone help pls?

BCasaleiro
  • 139
  • 8

4 Answers4

0

Turns out there was a problem in the facebook app configuration and administrators can't test their own app so i moved myself to developer and it all started working

BCasaleiro
  • 139
  • 8
0

Your missing onActivityResult where you are supposed to do this

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    myFacebookCallbackManager.onActivityResult(requestCode, resultCode, data);
}

I hope you mentioned the facebbook meta data and activity name in manifest. Also permissions, say like

<uses-permission android:name="android.permission.INTERNET" />
DJphy
  • 1,292
  • 1
  • 17
  • 31
0

I had same problem. Finally solved in this way:

  • Create Android app on Facebook developer and set it to public instead development mode.

  • Manifest:

        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"/>
    
  • Strings.xml

    XXXXXXXXXXXXXX

  • If you want to use fragment for Login the proper way is:

Step 1:

    CallbackManager mCallbackManager;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

        mCallbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(mCallbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    // App code
                    Log.e("Success", "Login");
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id, first_name, last_name ,email, gender, birthday");

                    GraphRequest gr = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject json, GraphResponse response) {
                                    if (response.getError() != null) {
                                        // handle error
                                        System.out.println("ERROR "+response.getError().toString());
                                    } else {
                                        //System.out.println("Success");
                                        try {

                                            String jsonresult = String.valueOf(json);
                                            System.out.println("JSON Result"+jsonresult);
                                            String resp = String.valueOf(response.getJSONObject());
                                            System.out.println("JSON resp "+resp);


                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }

                            });
                    gr.setParameters(parameters);
                    gr.executeAsync();

                }

                @Override
                public void onCancel() {
                    // App code
                    Log.e("onCancel", "Login");
                }

                @Override
                public void onError(FacebookException onError) {
                    // App code
                    Log.e("onError", "Login "+onError.getMessage());
                }
            });
    }

Step 2:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            mCallbackManager.onActivityResult(requestCode, resultCode, data);
        }
    }

Step 3: - inside OnClickListener

LoginManager.getInstance().logInWithReadPermissions(YourFragment.this, Arrays.asList("public_profile"));

I was using getActivity() and because of this every time something flashes instead showing Facebook Login view. Basically instead getActivity() have to use FragmentClaseName.this. I hope this will help u. Happy coding!!!

enam
  • 952
  • 11
  • 11
0

Facebook SS

I was also facing the same problem. All of my code was correct. Findaly found that my facebook application was in the developer mode. Thats why the login process was not working.

Amit Kundu
  • 119
  • 5