Trying to Log In with facebook but getting white screen everytime (only worked fine on first time LogIn)
- Facebook Api – compile ‘com.facebook.android:facebook-android-sdk:[4,5)’
-
There is no History tag in manifest
-
Tried checking current accessToken and there is none.
-
Tried on both Sandbox ON n OFF mode
LoginButton -> (Calls FacebookLogIn as startActivityforResult) ->In turn calls signIn or signOut methods and after that finish()
Please help I have no idea what is wrong in my code .
Main Activity – (Calling FacebookLogIn Activity for Result)
package com.example.firebasesignin;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.facebook.AccessToken;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
GoogleLogIn googleLogIn;
FacebookLogIn facebookLogIn;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
googleLogIn = new GoogleLogIn();
facebookLogIn = new FacebookLogIn();
findViewById(R.id.google).setOnClickListener(this);
findViewById(R.id.facebook).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
Profile profile = Profile.getCurrentProfile();
textView = (TextView) findViewById(R.id.textView);
if(profile!=null)
textView.setText(profile.getFirstName());
//textView.setText(Boolean.toString(isLoggedIn()));
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(user.getUid());
} else {
}
}
};
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.google:
Intent intent = new Intent(MainActivity.this, GoogleLogIn.class);
intent.putExtra("SignState", 1);
startActivityForResult(intent, 100);
break;
case R.id.facebook:
Intent intent2 = new Intent(MainActivity.this, FacebookLogIn.class);
intent2.putExtra("SignState", 1);
startActivityForResult(intent2, 102);
break;
case R.id.button2:
Intent intent1 = new Intent(MainActivity.this, GoogleLogIn.class);
intent1.putExtra("SignState", 0);
startActivityForResult(intent1, 101);
break;
case R.id.button3:
Intent intent3 = new Intent(MainActivity.this, FacebookLogIn.class);
intent3.putExtra("SignState", 0);
startActivityForResult(intent3, 103);
break;
}
}
/**
* Dispatch incoming result to the correct fragment.
*
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
if(resultCode==RESULT_CANCELED)
{
textView.setText("Failed");
}
} else if (requestCode == 101) {
if(resultCode==RESULT_OK)
{
textView.setText("Signed Out");
}
} else if (requestCode == 102) {
if(resultCode==RESULT_CANCELED)
{
int result = data.getIntExtra("Result",0);
if(result==1)
{
textView.setText("FB Cancelled");
}
if(result==2)
{
textView.setText("FB Error");
}
}
if (resultCode==RESULT_OK)
{
textView.setText(data.getStringExtra("Id"));
}
} else if (requestCode == 103) {
if(resultCode==RESULT_OK)
{
textView.setText("Signed Out");
}
}
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
public boolean isLoggedIn() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
return accessToken != null;
}
}
FacebookLogIn Activity —
package com.example.firebasesignin;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
/**
* Created by Dawn on 10/13/2016.
*/
public class FacebookLogIn extends AppCompatActivity {
CallbackManager callbackManager;
int status;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
status = intent.getIntExtra("SignState", 0);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
callbackManager = CallbackManager.Factory.create();
if (status == 1) {
signIn();
}
if (status == 0) {
signOut();
}
}
public void signIn() {
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
String id = loginResult.getAccessToken().getUserId();
Intent data1 = new Intent();
data1.putExtra("Result", 0);
data1.putExtra("Id",id);
setResult(RESULT_OK, data1);
finish();
}
@Override
public void onCancel() {
Intent data1 = new Intent();
data1.putExtra("Result", 1);
setResult(RESULT_CANCELED, data1);
finish();
}
@Override
public void onError(FacebookException exception) {
Intent data1 = new Intent();
data1.putExtra("Result", 2);
setResult(RESULT_CANCELED, data1);
finish();
}
});
}
public void signOut() {
FacebookSdk.sdkInitialize(getApplicationContext());
LoginManager.getInstance().logOut();
Intent data1 = new Intent();
data1.putExtra("Result", 0);
setResult(RESULT_OK, data1);
finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
finish();
}
}
2
Answers
I identified that first the white screen is of my app not facebook , second it is not returning to MainActivity is because Callback from Facebook is not being handled by Callbackmanager.
So digging around the web I found the soultion
While using LoginManager to trigger register callback we should add one more line of code before it
Change the read permissions you guyz require accordingly .
please try setting the “Sandbox Mode” to “OFF” in “Basic Info” in Settings of Facebook Apps let me know is it work for you or not?