Firebase在几次尝试后退出

时间:2016-10-19 23:48:18

标签: android firebase firebase-realtime-database firebase-authentication

我正在使用Firebase,并在signOut()中添加了onBackPressed()方法以返回登录活动。 但是当我按下“返回”以退出时,我会返回登录活动,onAuthStateChanged在登录后将我带回下一个活动状态。 不知怎的,我还是登录了。

按下后退按钮后第二次或第三次工作。 这真的很奇怪,我希望你能帮帮我.. 这是我的代码的一些片段:

这是登录后活动中的onBackPressed()

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();

         FirebaseAuth.getInstance().signOut();
         finish();

    }
}

这是登录活动中的AuthStateListener()

  mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                curr_user = user;

                Toast.makeText(LoginActivity.this, "yes", Toast.LENGTH_SHORT).show();
                if(curr_user.getDisplayName() == null)
                    autoSignIn("email");
                else
                    autoSignIn("google");

            user.getUid());
            } else {
                Toast.makeText(LoginActivity.this, "out", Toast.LENGTH_SHORT).show();                 
            }           
        }
    };

这是启动下一个活动的autoSignIn()方法:

public void autoSignIn(String accType) {

    if(accType.equals("email")) {

        mDatabaseReference.child("users").orderByKey().equalTo(curr_user.getUid());

        Query myTopPostsQuery = mDatabaseReference.child("users").orderByKey().equalTo(curr_user.getUid());
        myTopPostsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                                    editor = pref.edit();
                                    User user = snapshot.getValue(User.class);
                                    editor.putString("DisplayName", user.username);
                                    editor.commit();
                                    intent = new Intent(getApplicationContext(), MainActivity.class);
                                    intent.putExtra("userID", curr_user.getUid());
                                    startActivity(intent);
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
    else {
        Toast.makeText(this,"google", Toast.LENGTH_SHORT).show();

        txtWelcome.setText("Hello " + curr_user.getDisplayName());
        intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.putExtra("userID", curr_user.getUid());
        editor = pref.edit();
        editor.putString("DisplayName", curr_user.getDisplayName());
        editor.commit();
        startActivity(intent);

    }
}

2 个答案:

答案 0 :(得分:1)

figured this out eventually, Somehow after FirebaseAuth.getInstance().signOut();and finish() it runs the code inside onAuthStateChanged when user != null. I removed calling to the method autoSignIn() and it worked.

答案 1 :(得分:0)

退出后调用super.onBackPressed()。

#include <iostream>
#include <fstream>
#include <string>

using std::cout;

class Node
{
public:
    Node *prev;
    Node *next;
    int shares;
    float pps;
    Node(int vshares, float vpps)
    {
        shares = vshares;
        pps = vpps;
        prev = next = nullptr;
    }
};

class dlist
{
public:
    Node *head;
    Node *tail;

    dlist()
    {
        head = tail = nullptr;
    }
    ~dlist()
    {
        destroy();
    }

    void push_back(int shares, float pps) 
    {
        Node *node = new Node(shares, pps);
        if (head == NULL) 
        {
            head = tail = node;
        }
        else 
        {
            tail->next = node;
            node->prev = tail;
            tail = node;
        }
    }

    void destroy() 
    {
        Node *walk = head;
        while (walk) 
        {
            Node *node = walk;
            walk = walk->next;
            delete node;
        }
        head = tail = nullptr;
    }
};

class stack
{
public:
    int maxsize;
    int count;
    dlist list;
    stack(int size)
    {
        count = 0;
        maxsize = size;
    }

    void push(int num, float price)
    {
        if (count < maxsize)
        {
            list.push_back(num, price);
            count++;
        }
    }

    void pop()
    {
        Node *tail = list.tail;
        if (!tail)
        {
            //already empty
            return;
        }

        if (tail == list.head)
        {
            //only one element in the list
            delete tail;
            list.head = list.tail = nullptr;
            count--;
        }
        else
        {
            Node *temp = list.tail->prev;
            delete list.tail;
            list.tail = temp;
            list.tail->next = nullptr;
            count--;
        }
    }

    void display()
    {
        Node  *walk = list.head;
        while (walk)
        {
            cout << "(" << walk->shares << "," << walk->pps << ") ";
            walk = walk->next;
        }
        cout << "\n";
    }
};

int main()
{
    stack s(3);
    s.push(101, 0.25f);
    s.push(102, 0.25f);
    s.push(103, 0.25f);
    s.push(104, 0.25f);
    s.display();
    s.pop();
    s.display();
    return 0;
}