使用Firebase无法在Android上正确退出Facebook

时间:2017-04-07 08:47:49

标签: android facebook firebase firebase-authentication facebook-authentication

我尝试登录Facebook,它正在运行。但是当我退出时,Facebook的“退出”按钮仍然显示在我的登录活动中,当我点击它时,我可以退出。但我不想这样做。

但真正的问题是,如何正确退出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;

}

3 个答案:

答案 0 :(得分:1)

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
        }

答案 1 :(得分:1)

当您使用firbase注销时,您应该使用它来调用facebook Loginmanager。 bcz firebase不会退出你的脸书。它只是从firebase中签出你的身份证明。

library(shiny)

# Define UI
ui <- fluidPage(
  # Application title
  titlePanel("Identify A, B and C"),
  sidebarLayout(
    sidebarPanel(width = 5,
                 helpText("Present a statement and receive a response: 1 is a Knight who always tells the truth, 2 is a Knave who always lies, and 3 is a Normal who can do either."),
                 # Number of Questions
                 numericInput(inputId = "Questions", label = "Number of Questions", 
                              value = 1, min = 1, max = 10, step = 1),
                 splitLayout(cellWidths = c("25%","70%"), 
                             style = "border: 1px solid silver;",
                             cellArgs = list(style = "padding: 3px"),
                             uiOutput("textQuestions"), uiOutput("textQuestions2"))
    ),
    mainPanel(
      # Right hand side output
    )
  )
)

# Define server logic 
server <- function(input, output) {
  ####### I don't want these to delete initially everytime??
  output$textQuestions <- renderUI({
    Questions <- as.integer(input$Questions)
    lapply(1:Questions, function(i) {
      textInput(inputId = paste0("Who", i), label = paste0(i, ". Ask:"), placeholder = "A")
    })
  })
  ########
  output$textQuestions2 <- renderUI({
    Questions <- as.integer(input$Questions)
    lapply(1:Questions, function(i) {
      textInput(inputId = paste0("Q", i) , label = paste0("Logic:"), 
                value = "", placeholder = "A == 1 & (B != 2 | C == 3)")
    })
  })
  ######
}

# Run the application 
shinyApp(ui = ui, server = server)

你应该同时使用两个注销。否则它不会从Facebook注销。

答案 2 :(得分:0)

finish();
        firebaseAuth.signOut();
        LoginManager.getInstance().logOut();
        startActivity(new Intent(MainActivity.this, Giris.class));

之后你必须调用finish()作为永远不会达到的代码aftr。