1

I created the Activity with the Wizard. I deleted what I am not interested in, for example, the floating action button.

Created my custom items in the activity_main_drawer.xml

The wizard created the onNavigationItemSelected function (see code below) and I added my code so it quits the activity and goes back to the Login Screen.

So I don't know why, I see nothing interesting in the logcat so this is a logical problem. I am a newbie and Navigation View scares me a lot... Let me know if I missed anything you need.

package com.skrefi.googleplaycopy;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private FirebaseAuth firebaseAuth;

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

        firebaseAuth = FirebaseAuth.getInstance();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }


    //Don't know what this is
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    //Don't know what this is either
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //In case I need it for the admin activity >> the 3 dots pop down thingy <<
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        MakeToast("SOMETHING");

        switch (item.getItemId()){
            case R.id.action_settings:
                MakeToast("Settings button clicked");
                return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        switch(item.getItemId()){
            case R.id.item_signout:
                firebaseAuth.getInstance().signOut();
                MakeToast("Signed out!");
                finish();
                startActivity(new Intent(MainActivity.this, LoginActivity.class));
                break;
        }

        //DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        //drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    public void MakeToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_LONG).show();
    }
}

Edit:

So it may be confusing if I set the visibility to gone and I get use the layout I want right in this layout file because I get the screen without the toolbar, this is what I want. AND I want to keep that as well for the admin part, to check later in if the user is me, and if so, I get a new item in the drawer called Admin Activity which simply set the thingy visibility to visible so i get the admin screen.

Here is what you (@) asked for:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!--This is for a potential admin screen-->

    <include
        android:id="@+id/include_toolbar_with_threedots_settings"
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is for test"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

</android.support.v4.widget.DrawerLayout>
SKREFI
  • 177
  • 2
  • 13
  • 1
    Please post the layout that has the `` in it. – Mike M. Jan 25 '19 at 19:54
  • @MikeM. Edited the question, check the end. Thank you. :) – SKREFI Jan 25 '19 at 21:14
  • 1
    Move the `` to after the ``. The drawer in a `` must be listed last to work correctly. Also, you really should have only one main content `View`. That is, you should combine that `` and `` into one, somehow. Otherwise, one of them is going to completely cover the other one, and you will likely have problems interacting with the one in back. – Mike M. Jan 25 '19 at 21:22
  • @MikeM. **navigationView.bringToFront();** Found this on the already asked question (really searched but didn't find that question). So the actual problem was it was not "the first one" That's... wow.. Thank you! – SKREFI Jan 25 '19 at 21:27
  • 1
    Yeah, you can do that in code, but it's kinda hacky, and if you arrange your layout correctly, you don't need to do it at all. Anyhoo, glad you got it working. Cheers! – Mike M. Jan 25 '19 at 21:29

1 Answers1

0

You're calling finish() before actually starting your activity while you should call it after starting your login activity. The following code will start Login activity and close the Main activity.

@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        switch(item.getItemId()){
            case R.id.item_signout:
                firebaseAuth.getInstance().signOut();
                MakeToast("Signed out!");
                startActivity(new Intent(MainActivity.this, LoginActivity.class));
                finish();
                break;
        }

    //DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    //drawer.closeDrawer(GravityCompat.START);
    return true;
}
113408
  • 3,364
  • 6
  • 27
  • 54
  • Actually, the entire function is executed, it doesn't work like return or something, [here] (https://stackoverflow.com/questions/10847526/what-exactly-activity-finish-method-is-doing) is what **finish()** does. I am using my way in other activities so I know this is not a problem. Even tho, given that, why isn't the Toast showing? – SKREFI Jan 25 '19 at 21:21