Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open next activity only after navigation drawer completes it closing animation

Tags:

android

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_left_out animation as transition between activities.

The code works, but these animations conflicts with the closing animation of Navigation Drawer, as even before the drawer gets completely closed, the current activity starts sliding out to left & next activity starts sliding in from right.

So, is there any way to start the animation only after drawer is completely closed?

Thank You

like image 703
collin Avatar asked Mar 14 '14 13:03

collin


2 Answers

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

        }
    });
like image 171
sHaRkBoY Avatar answered Oct 05 '22 04:10

sHaRkBoY


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;
}
like image 31
Mariusz Avatar answered Oct 05 '22 06:10

Mariusz