底部导航栏按钮作为Signout google firebase

时间:2017-07-10 20:59:06

标签: android firebase button firebase-authentication bottomnavigationview

我想使用底部导航栏按钮作为Google Firebase身份验证的注销。我已经有按钮,我正在使用该按钮作为退出,但现在我想使用底部导航栏按钮作为注销按钮。意图没有直接工作,请帮我怎么做

public class second extends AppCompatActivity {
private Button mlogout;




private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
//bottom nav

    BottomNavigationView bottomNavigationView=(BottomNavigationView) findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){

                case R.id.action_profile:
                    Toast.makeText(second.this,"hello how are u",Toast.LENGTH_SHORT).show();

                    break;
                case R.id.action_home:
                    Toast.makeText(second.this,"hello how are u",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.action_add:
                    Toast.makeText(second.this,"hello how are u",Toast.LENGTH_SHORT).show();

                    break;
            }
            return true;
        }
    });
    //bottom nav



    mAuth=FirebaseAuth.getInstance();


    mAuthListener =new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (firebaseAuth.getCurrentUser()==null){
                startActivity(new Intent(second.this,MainActivity.class));

            }
        }
    };

    mlogout=(Button) findViewById(R.id.logout);

    mlogout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mAuth.signOut();

        }
    });

}

@Override
protected void onStart() {
    super.onStart();

    mAuth.addAuthStateListener(mAuthListener);
}

}

,xml文件是

android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:id="@+id/drawerLayout"
tools:context="com.food.sheenishere.stark.second">

<Button
    android:id="@+id/logout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="log out"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="122dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="368dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="8dp"
    android:visibility="visible"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@drawable/nav_item_color_state"
    app:itemTextColor="@drawable/nav_item_color_state"
    app:layout_constraintBottom_toBottomOf="parent"
    app:menu="@menu/bottom_navigation_main"
    tools:layout_editor_absoluteX="8dp" />

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

1。bottom_navigation_main菜单中,只需为Item选项添加LogOut即可。

<强> bottom_navigation_main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_profile" />

    <item
        android:id="@+id/action_home" />

    <item
        android:id="@+id/action_add" />

    <item
        android:id="@+id/action_logout"
        android:title="Log Out"
        android:icon="@drawable/ic_logout"
        app:showAsAction="always"/>
</menu>

2。second活动的onNavigationItemSelected()方法中,检查天气是否选定了R.id.action_logout项。如果选中,则调用注销方法mAuth.signOut()。更新您的onCreate()方法,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    // Firebase
    mAuth = FirebaseAuth.getInstance();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (firebaseAuth.getCurrentUser()==null){
                startActivity(new Intent(second.this,MainActivity.class));
            }
        }
    };

    // Bottom Navigation
    BottomNavigationView bottomNavigationView=(BottomNavigationView) findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){

                case R.id.action_profile:
                    Toast.makeText(second.this, "Profile", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.action_home:
                    Toast.makeText(second.this, "Home", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.action_add:
                    Toast.makeText(second.this, "Add", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.action_logout:
                    Toast.makeText(second.this, "Log Out", Toast.LENGTH_SHORT).show();

                    // LogOut
                    mAuth.signOut();
                    break;
            }

            return true;
        }
    });
}

希望它有所帮助!

相关问题