Skip to content Skip to sidebar Skip to footer

Open Next Activity Only After Navigation Drawer Completes It Closing Animation

I'm using Navigation Drawer in my application. When the user clicks on any of the menu item in drawer, it opens a new Activity (not fragment). Now, I'm using slide_right_in/slide_l

Solution 1:

You can open Activity with a delay. For example, in such a way Activity will be started after 250ms:

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Intent intent = new Intent(<filter>);
        startActivity(intent);
        finish();
    }
}, 250);
mDrawerLayout.closeDrawer(mDrawerList);

Solution 2:

I did this somehow similiar to Jan.

Select an item

If an item gets clicked, i save its id and close the Drawer:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
    {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem)
        {
            clickedItem = menuItem.getItemId();
            drawerLayout.closeDrawers();
            return true;
        }
    });

Listen for drawer close

If the Drawer gets closed i listen for it and check if an item has been clicked. If it has i call my method to handle the navigation click.

drawerToggle = new ActionBarDrawerToggle(activity, drawerLayout, R.string.accessibility_open_nav, R.string.accessibility_open_nav)
    {
        @Override
        public void onDrawerClosed(View drawerView)
        {
            super.onDrawerClosed(drawerView);

            if(clickedItem != 0)
            {
                handleNavigationClick();
            }
        }
    };

drawerLayout.setDrawerListener(drawerToggle);

Handle the click

Here i react to the item click on open intents in this case (simplified). You need to reset the clickedItem here to 0. That is, because if you go back to an activity, open and close the drawer, it would still have the clickedItem number and would handle the click again.

private void handleItemClick()
{
    switch (clickedItem)
    {
        case R.id.item_1:
            do_something_1;
            break;

        case R.id.item_2:
            do_something_2;
            break;
    }

    clickedItem = 0;
}

Solution 3:

Every answer is so complicated..... It's so easy.

Just add a drawerlistener and do something in onClosed() method:

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

when you select a item from navigation drawer you will call this method to close the drawer:

drawer.closeDrawer(GravityCompat.START);

after the above method call just add below lines and do whatever you want:

drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

        }

        @Override
        public void onDrawerOpened(@NonNull View drawerView) {

        }

        @Override
        public void onDrawerClosed(@NonNull View drawerView) {
            startActivity(finalIntent);

           // Or else do something here....
        }

        @Override
        public void onDrawerStateChanged(int newState) {

        }
    });

Solution 4:

Don't close navigation drawer. It will slide with the old activity. Or call startActivity after drawerLayout.closeDrawer(drawerList);

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    drawerLayout.closeDrawer(drawerList);
    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    //set animation here
    startActivity(intent);
    finish();
}

Solution 5:

The nikis's answer does not cover all cases. If your Fragment or Activity contains map or CameraView it may costs more time. And differences between simple screen and screen with map too big so it's hard to select a good delay.

The best approach is to send callback to NavigationDrawer from just opened Activity/Fragment in onResume(). Then close drawer in this callback.


Post a Comment for "Open Next Activity Only After Navigation Drawer Completes It Closing Animation"