简体   繁体   English

底部导航栏按钮,以“注销Google Firebase”

[英]Bottom navigation bar Button as Signout google firebase

I want to use Bottom navigation bar button as sign out for the google Firebase authentication. 我想使用底部导航栏按钮作为Google Firebase身份验证的注销。 I already have button and I am using that button as sign out but now i want to use bottom navbar button as signout button. 我已经有按钮,并且我正在使用该按钮作为注销,但是现在我想使用底部导航栏按钮作为注销按钮。 Intents are not working directly, please help me how to do it 意图无法直接发挥作用,请帮助我该怎么做

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);
}

}

and the xml file is 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. Inside your bottom_navigation_main menu, just add an Item for LogOut option. 1.bottom_navigation_main菜单内,只需添加一个Item for LogOut选项。

bottom_navigation_main.xml 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. In your second activity's onNavigationItemSelected() method, check weather the item R.id.action_logout selected or not. 2.second活动的onNavigationItemSelected()方法中,检查天气R.id.action_logout选中了R.id.action_logout项目。 If selected then call logout method mAuth.signOut() . 如果选择,则调用注销方法mAuth.signOut() Update your onCreate() method as below: 如下更新您的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;
        }
    });
}

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jetpack compose 中的底部导航栏重叠按钮 - Bottom navigation bar overlap button in jetpack compose 浮动操作按钮块底部导航栏 - floating action button blocks bottom navigation bar 底部导航栏 - 矩形操作按钮 - Bottom navigation bar - rectangle action button Google 相册应用底部导航栏行为 - Google Photos app bottom navigation bar behavior 如何一起使用底部导航栏和侧边导航栏,我的侧边导航按钮不显示时我但底部导航 - How to use bottom navigation bar and side navigation bar together, my side navigation button is not showing when i but bottom navigation 单击后退按钮时,在底部导航栏中更改动画状态 - Changing animation state in bottom navigation bar while clicking back button 安卓 如何在底部添加按钮(导航栏) - Android. How to add button to the bottom (navigation bar) 隐藏在底部导航栏后面的 Android 浮动操作按钮 - Android floating action button hidden behind of bottom navigation bar 如何在带有缺口的底部导航栏中心添加浮动操作按钮? - How to add Floating action Button in Bottom Navigation Bar Center with notch? 隐藏Android屏幕底部的导航栏“更多”按钮 - hide the navigation bar “more” button at the bottom of the screen on Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM