0

I have an activity which has a navigationView and a toolbar (I set toolbar name manually).

My problem is; when I open the navigationView, it goes out of activity and back to previous activity.

I tried many ways to find the problem and I found when i wasn't using this line the problem has been solved and navigation menu opens without any problem.

setSupportActionBar(toolbar);

I don't know which one the problem is? toolbar or navigation?

Note : There is nothing special about the styles.

Activity :

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

        setSupportActionBar(toolbar);//Set actionBar with toolbar

        if(getSupportActionBar() != null)
        {
            getSupportActionBar().setDisplayShowTitleEnabled(false);//Remove actionBar title
            toolbar.setTitle(mClassEntry.getClassName());
        }

        DrawerLayout drawer = findViewById (R.id.drawerLayout_StudentList);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this , drawer , toolbar , 0b0, 0);
        toggle.syncState();

        NavigationView drawerItems = findViewById (R.id.navigationView_StudentList);
        drawerItems.setNavigationItemSelectedListener(item -> {
            .
            .
            .
        }

Xml :

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.user.classmanager.StudentActivity"
    android:id="@+id/drawerLayout_StudentList"
    style="@style/DrawerLayout_all"
    >

    <FrameLayout
        android:id="@+id/frameLayout_StudentList"
        style="@style/Width_height_all_both_matchParent"
        >
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_StudentList"
            style="@style/ToolBar_all"
            />
        <RelativeLayout
            style="@style/RelativeLayout_searchView_all"
            >
            <android.support.v7.widget.SearchView
                android:id="@+id/searchView_StudentList"
                style="@style/SearchView_all"
                />
        </RelativeLayout>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView_StudentList"
            style="@style/RecyclerView_FrameLayout_all"
            />
    </FrameLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView_StudentList"
        style="@style/NavigationView_StudentList"
        />
</android.support.v4.widget.DrawerLayout>

Also my toolbar has a icon to close activity :

public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.toolbar_backicon, menu);//Back item in Toolbar

    return super.onCreateOptionsMenu(menu);
}

public boolean onOptionsItemSelected(MenuItem item)
{
    finish();//close activity

    return super.onOptionsItemSelected(item);
}
elyar abad
  • 771
  • 1
  • 8
  • 27
Nima Khalili
  • 251
  • 7
  • 18
  • The `ActionBarDrawerToggle` handles setting the click listener on that button to toggle the drawer state. I would have to guess that either you're calling something after the `ActionBarDrawerToggle` setup that's overriding that behavior, or somehow that button isn't causing the issue itself; e.g., something's overlapping it that's closing the `Activity` upon a click, some Exception is closing the current `Activity`, etc. Is there anything in the logs? – Mike M. Aug 24 '19 at 07:41
  • Please share logcat. – Fazal Jarral Aug 24 '19 at 07:48
  • Btw, you don't _have_ to set the `Toolbar` as the support `ActionBar`. It's not mandatory. – Mike M. Aug 24 '19 at 07:49
  • i found where is problem. i have a btn in toolbar and when i setActionBar with my toolbar this problem arises. @ Mike M – Nima Khalili Aug 24 '19 at 08:33
  • But i don't know how to handle that correctly – Nima Khalili Aug 24 '19 at 08:34
  • Check the `MenuItem`'s ID before you call `finish()`; e.g., `if (item.getItemId() == R.id.your_menu_close_id) { finish(); }`. – Mike M. Aug 24 '19 at 08:41
  • I did it and now Navigation menu doesn't close activity but do not anything too(can't open). – Nima Khalili Aug 24 '19 at 08:51
  • That doesn't really sound right for the code you've posted. Anyway, from your current description, you'll probably have to check `toggle.onOptionsItemSelected()` in `onOptionsItemSelected()`, like is shown in the first code block in [this answer](https://stackoverflow.com/a/36570774). You'll need to declare `ActionBarDrawerToggle toggle` as a field – i.e., outside of any method, at the top of your `Activity` – then remove `ActionBarDrawerToggle` from the beginning of the line you show in the question. – Mike M. Aug 24 '19 at 09:08
  • Thank you, your guidance is correct but i found a solution too. I create an `imageView` in my `toolbar` then put close activity in onClick methode for imageView.In that case no need to use `onOptionsItemSelected` and no problem for `NavigationView`.Which way is better you think?@Mike M – Nima Khalili Aug 24 '19 at 12:14
  • Whatever works for you, really. It's basically all just responding to simple click events, so however you want to handle those is likely fine. I'm still not sure of your exact setup, but as long as what you've got isn't causing problems, then setting a click listener on an `ImageView` is pretty much equivalent to setting one on a menu item. – Mike M. Aug 24 '19 at 13:03
  • Your answers helped me so much. Thank you – Nima Khalili Aug 24 '19 at 13:31

1 Answers1

0

in

public boolean onOptionsItemSelected(MenuItem item)
{
    finish();//close activity

return super.onOptionsItemSelected(item);
}

you must specify the id of your close button so that the activity only closes when you click on the icon on the close button . In this you activity will close everytime , so must replace the code to something like this

  public boolean onOptionsItemSelected(MenuItem item)
{
    if(item.getId()==R.id.closeButton)

 {        finish();//close activity
  return true;
   } 

return super.onOptionsItemSelected(item);
}