无法使用 Firebase 在 Android 上正确退出 Facebook
Can't properly sign out of Facebook on Android with Firebase
我尝试登录 Facebook,它正在运行。但是当我注销时,Facebook 的 "Sign Out" 按钮仍然出现在我的登录 activity 中,当我点击它时,我可以注销。但是我不想那样做。
但真正的问题是,如何正确退出 Facebook?我同时使用 Firebase 身份验证和 Facebook 身份验证(支持 Firebase)
Giris.java(登录)
public class Giris extends AppCompatActivity implements View.OnClickListener {
private FancyButton buttonSignIn;
private EditText editTextEmail;
private EditText editTextPassword;
private TextView textViewSignup, textViewSifreUnuttum, girismesaji;
public boolean cancel = false;
public boolean isFirstStart;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private Dialog progressDialog;
private CallbackManager mCallbackManager;
private static final String TAG = "FacebookLogin";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_giris);
FacebookSdk.sdkInitialize(getApplicationContext());
firebaseAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// Kullanıcı oturumu açtı
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
Log.d("onCreate", "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// Kullanıcı oturumu kapattı.
Log.d("onCreate", "onAuthStateChanged:signed_out");
}
}
};
firebaseAuth.addAuthStateListener(mAuthListener);
mCallbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
}
@Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data)
{
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
AuthCredential credential =
FacebookAuthProvider.getCredential(token.getToken());
progressDialog.show();
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new
OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
Log.d(TAG, "signInWithCredential:onComplete:" +
task.isSuccessful());
if(task.isSuccessful())
{
finish();
Intent fGiris=new Intent(Giris.this,
MainActivity.class);
fGiris.putExtra("giris","facebook");
startActivity(fGiris);
}
else
{
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(Giris.this, "Facebook ile bağlantı başarız oldu.",
Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
}
else
{
Intent hata = new Intent(Giris.this, InternetCon.class);
startActivity(hata);
}
MainActivity.java(注销)
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
private Fragment fragment;
private FragmentManager fragmentManager;
public TextView tarih;
final String PREFS_NAME = "MyPrefsFile";
final String SHAREDPREF_DATE = "SharedPrefDate";
private FirebaseAuth firebaseAuth;
private FirebaseUser user;
private TextView textViewUserEmail;
public CircleImageView userImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser()==null)
{
finish();
startActivity(new Intent(this,Giris.class));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0); //giriste gelen maili Navigation header'a at
textViewUserEmail= (TextView) headerView.findViewById(R.id.textKullaniciBilgisi);
textViewUserEmail.setText(firebaseAuth.getCurrentUser().getEmail());
userImage= (CircleImageView) findViewById(R.id.kullaniciNavHesapResmi);
fragmentManager = getSupportFragmentManager();
fragment = new AraclarimFragment();
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.main_container, fragment).commit();
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Navigation itemlerinin click olaylarını burada yapıyoruz.
int id = item.getItemId();
if (id == R.id.nav_cikis)
{
Intent gelen=getIntent();
String loginType=gelen.getStringExtra("giris");
if(loginType=="facebook")
{
finish();
firebaseAuth.signOut();
LoginManager.getInstance().logOut();
startActivity(new Intent(MainActivity.this, Giris.class));
}
else
{
finish();
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MainActivity.this, Giris.class));
}
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
finish();
firebaseAuth.signOut();
LoginManager.getInstance().logOut();
startActivity(new Intent(MainActivity.this, Giris.class));
你必须在之后调用 finish(),因为代码永远不会到达。
if (id == R.id.nav_cikis)
{
Intent gelen=getIntent();
String loginType=gelen.getStringExtra("giris");
if(loginType=="facebook")
{
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
startActivity(new Intent(MainActivity.this, Giris.class));
finish();
}
else{
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
startActivity(new Intent(MainActivity.this, Giris.class));
finish();// must be the last line
}
当您使用 firbase 注销时,您应该使用它调用 facebook Loginmanager。 bcz firebase 不会注销你的 facebook。它只是从 firebase 注销您的身份验证。
public void logout() {
try {
if (isInternetPresent) {
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
System.exit(0);
} else {
Toast.makeText(MainActivity.this, "No Internet", Toast.LENGTH_LONG).show();
}
} catch (RuntimeException e) {
e.printStackTrace();
}
}
你应该同时使用这两个注销。否则它不会退出Facebook。
我尝试登录 Facebook,它正在运行。但是当我注销时,Facebook 的 "Sign Out" 按钮仍然出现在我的登录 activity 中,当我点击它时,我可以注销。但是我不想那样做。
但真正的问题是,如何正确退出 Facebook?我同时使用 Firebase 身份验证和 Facebook 身份验证(支持 Firebase)
Giris.java(登录)
public class Giris extends AppCompatActivity implements View.OnClickListener {
private FancyButton buttonSignIn;
private EditText editTextEmail;
private EditText editTextPassword;
private TextView textViewSignup, textViewSifreUnuttum, girismesaji;
public boolean cancel = false;
public boolean isFirstStart;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private Dialog progressDialog;
private CallbackManager mCallbackManager;
private static final String TAG = "FacebookLogin";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_giris);
FacebookSdk.sdkInitialize(getApplicationContext());
firebaseAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// Kullanıcı oturumu açtı
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
Log.d("onCreate", "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// Kullanıcı oturumu kapattı.
Log.d("onCreate", "onAuthStateChanged:signed_out");
}
}
};
firebaseAuth.addAuthStateListener(mAuthListener);
mCallbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
}
@Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data)
{
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
AuthCredential credential =
FacebookAuthProvider.getCredential(token.getToken());
progressDialog.show();
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new
OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
Log.d(TAG, "signInWithCredential:onComplete:" +
task.isSuccessful());
if(task.isSuccessful())
{
finish();
Intent fGiris=new Intent(Giris.this,
MainActivity.class);
fGiris.putExtra("giris","facebook");
startActivity(fGiris);
}
else
{
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(Giris.this, "Facebook ile bağlantı başarız oldu.",
Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
}
else
{
Intent hata = new Intent(Giris.this, InternetCon.class);
startActivity(hata);
}
MainActivity.java(注销)
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
private Fragment fragment;
private FragmentManager fragmentManager;
public TextView tarih;
final String PREFS_NAME = "MyPrefsFile";
final String SHAREDPREF_DATE = "SharedPrefDate";
private FirebaseAuth firebaseAuth;
private FirebaseUser user;
private TextView textViewUserEmail;
public CircleImageView userImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser()==null)
{
finish();
startActivity(new Intent(this,Giris.class));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0); //giriste gelen maili Navigation header'a at
textViewUserEmail= (TextView) headerView.findViewById(R.id.textKullaniciBilgisi);
textViewUserEmail.setText(firebaseAuth.getCurrentUser().getEmail());
userImage= (CircleImageView) findViewById(R.id.kullaniciNavHesapResmi);
fragmentManager = getSupportFragmentManager();
fragment = new AraclarimFragment();
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.main_container, fragment).commit();
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Navigation itemlerinin click olaylarını burada yapıyoruz.
int id = item.getItemId();
if (id == R.id.nav_cikis)
{
Intent gelen=getIntent();
String loginType=gelen.getStringExtra("giris");
if(loginType=="facebook")
{
finish();
firebaseAuth.signOut();
LoginManager.getInstance().logOut();
startActivity(new Intent(MainActivity.this, Giris.class));
}
else
{
finish();
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MainActivity.this, Giris.class));
}
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
finish();
firebaseAuth.signOut();
LoginManager.getInstance().logOut();
startActivity(new Intent(MainActivity.this, Giris.class));
你必须在之后调用 finish(),因为代码永远不会到达。
if (id == R.id.nav_cikis)
{
Intent gelen=getIntent();
String loginType=gelen.getStringExtra("giris");
if(loginType=="facebook")
{
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
startActivity(new Intent(MainActivity.this, Giris.class));
finish();
}
else{
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
startActivity(new Intent(MainActivity.this, Giris.class));
finish();// must be the last line
}
当您使用 firbase 注销时,您应该使用它调用 facebook Loginmanager。 bcz firebase 不会注销你的 facebook。它只是从 firebase 注销您的身份验证。
public void logout() {
try {
if (isInternetPresent) {
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
System.exit(0);
} else {
Toast.makeText(MainActivity.this, "No Internet", Toast.LENGTH_LONG).show();
}
} catch (RuntimeException e) {
e.printStackTrace();
}
}
你应该同时使用这两个注销。否则它不会退出Facebook。