skip to Main Content

So im currently making an app and im using side menu for some features like logout, settings etc. Tho When the user needs to logout i have to redirect him to LoginActivity and for some reason it just closes the side menu like there is nothing on that item.
I’ve looked for similar problems here and on youtube but nothing helps the situation.Also the app seems to have no errors when the "logout_btn" on side menu is pressed

MainActivity:


public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private BottomNavigationView bottomNav;
    private DrawerLayout root;
    private Toolbar toolbar;
    private TextView report_btn;
    private NavigationView navigationView;

    private String[] permissions;
    private LocationManager locationManager;
    private FirebaseAuth mAuth;

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()){
            case R.id.logout_btn:
                mAuth.signOut();
                startActivity(new Intent(MainActivity.this, LoginActivity.class));
                break;
        }
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Making sure no data are left
        ServiceSharedInfo.deleteALlData();
        mAuth = FirebaseAuth.getInstance();

        //Setting values
        FirebaseUser user = mAuth.getCurrentUser();

        if(user == null){
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
        }else{
            System.out.println("#~#~#~#~#~#~# User: "+ user.getEmail());
        }

        permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET};
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        root = findViewById(R.id.root_layout);
        toolbar = findViewById(R.id.main_tool_bar);
        navigationView = findViewById(R.id.side_nav);
        bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);
        report_btn = findViewById(R.id.report_btn);

        setSupportActionBar(toolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(MainActivity.this,
                 root,toolbar,R.string.navigation_open,
                    R.string.navigation_close);
        root.addDrawerListener(toggle);
        toggle.syncState();

        getSupportFragmentManager().beginTransaction().replace(R.id.container_layout,
                new HomeFragment()).commit();
        bottomNav.getMenu().getItem(1).setChecked(true);
        report_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent emailIntent = new Intent(Intent.ACTION_SEND);

                emailIntent.setType("plain/text");
                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "<-Your subject here->");
                emailIntent.putExtra(Intent.EXTRA_TEXT, "<-Your Problem here->");

                startActivity(Intent.createChooser(emailIntent,"Send mail..."));
            }
        });
        navigationView.setNavigationItemSelectedListener(this::onNavigationItemSelected);

    }
    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment fragmentSelected = null;

                    switch(item.getItemId()){
                        case R.id.home_nav:
                            fragmentSelected = new HomeFragment();
                            break;
                        case R.id.installation_nav:
                            fragmentSelected = new InstallationFragment();
                            break;
                        case R.id.damage_nav:
                            fragmentSelected = new DamageFragment();
                            break;
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.container_layout,
                            fragmentSelected).commit();

                    return true;
                }
            };
}


activity_main:


<?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">

    <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" />

    <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>

</androidx.drawerlayout.widget.DrawerLayout>

side_nav_menu:


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

    <group>
        <item
            android:id="@+id/logout_btn"
            android:title="Log out :("
            android:icon="@drawable/ic_baseline_exit_to_app_24"/>
    </group>

</menu>

2

Answers


  1. Chosen as BEST ANSWER

    Ive been searching all day long and found out that the reason items in the side menu dont click is beacuse you have to put the "NavigationView" before the last closing attribute in your xml file. Not sure why but it worked !


  2. @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
         if (item.getTitle().equals("Log out :(")) {
                mAuth.signOut();
                startActivity(new Intent(MainActivity.this, LoginActivity.class));
                return false;
         }
    }
    

    This work for me..

    Try This…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search