You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用侧边菜单退出登录按钮无法跳转至LoginActivity问题求助

问题根因

你的DrawerLayout子控件摆放顺序不符合官方规范,DrawerLayout要求第一个子元素为主内容布局,第二个及之后的子元素为抽屉导航控件,你当前布局把NavigationView放在了主内容RelativeLayout的前面,导致侧边菜单的点击事件被上层主内容布局拦截,只会触发抽屉关闭逻辑,不会回调onNavigationItemSelected方法。

修复方案

1. 调整activity_main布局中DrawerLayout的子元素顺序

把主内容RelativeLayout移到NavigationView前面,修改后的布局结构如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/root_layout"
    android:fitsSystemWindows="true">

    <!-- 第一个子元素放主内容布局 -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:id="@+id/appBar">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/main_tool_bar">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="30dp"
                    android:layout_alignParentStart="true"
                    android:src="@drawable/somephoto" />

                <TextView
                    android:id="@+id/report_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:drawableStart="@drawable/ic_baseline_priority_high_24" />

            </RelativeLayout>

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

        <FrameLayout
            android:id="@+id/container_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/bottom_navigation"
            android:layout_below="@id/appBar" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        android:clickable="false"
        app:menu="@menu/button_navigation" />

    </RelativeLayout>

    <!-- 主内容之后放抽屉导航控件 -->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/side_nav"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/menu_header"
        app:menu="@menu/side_nav_menu"
        tools:visibility="gone" />

</androidx.drawerlayout.widget.DrawerLayout>

2. 补充跳转后的页面销毁逻辑(可选优化)

在onNavigationItemSelected的退出登录逻辑里,加一行finish()销毁当前MainActivity,避免用户按返回键回到主页面:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch(item.getItemId()){
        case R.id.logout_btn:
            mAuth.signOut();
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
            finish(); // 新增这行
            break;
    }
    // 可选:处理完点击事件后主动关闭抽屉
    root.closeDrawer(GravityCompat.START);
    return true;
}
验证方法

修改后运行应用,点击侧边栏的退出按钮,即可正常触发登出逻辑并跳转到登录页。

内容的提问来源于stack exchange,提问作者Xlorokoptiko Gia Gkazon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 13:54:02