Search code examples
androidandroid-activitynavigation-drawer

Why is my Navigation drawer menu opens other activities automatically without clicking the buttons?


What i have is left navigation menu that i have made using navigation drawer and i want it's button to open activities instead of fragments .. but the weird thing is that when the main activity opens which has the menu it open the second activity direct without displaying the main activity .. here is my code :

private void displayView(int position) {

    Intent intent = null;
    switch (position) {
    case 0:
        intent = new Intent(MainActivity.this, Gallery.class);
        break;
     ....
    if(intent != null) {
        mDrawerLayout.closeDrawer(mDrawerList);
        setTitle(navMenuTitles[position]);
        startActivity(intent);
    }
    else {
        // error in creating activity
        Log.e("MainActivity", "Error in creating Activity");
    }
}

And i call it from here :

mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }
    }

So why this is happening? can anyone help me?


Solution

  • In generally, the navigation drawer is initialized with some code when the activity starts(to show one of the items/do something). If, when you start the activity holding this navigation drawer, you immediately go the one of the activities pointed by the navigation drawer items then you need to check your code to see if you're not by any chance calling the displayView() method when the activity starts(which will start the other activity).