Navigation drawer does not close when calling clos

2019-05-11 01:12发布

I am trying to close the navigation drawer when an item in the navigation drawer's list is clicked. I have 2 activities A and B. The below code is the same for both A and B:

final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ListView mDrawerList = (ListView) findViewById(R.id.left_drawer);

// Set the list's click listener
mDrawerList.setOnItemClickListener(new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
        Intent intent = null;

        // first just close the drawer
        mDrawerLayout.closeDrawers();

        if (position == 0) { // Activity A
            intent = new Intent(v.getContext(), ActivityA.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        }
        else if (position == 1) { // Activity B
            intent = new Intent(v.getContext(), Activity B.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        }
    }
});

What I want to do is close the navigation drawer when the user navigate away from the activity so that when he comes back, the drawer is closed and does not stay opened.

However, the above code doesn't work:

  • If I'm in activity A, and I go to activity B, then from B I go back to A (all via the navigation drawer), the drawer in A stays opened

  • but if I'm in activity A, and I click on the item for activity A, I see the drawer closed just fine

I tried to do some logging:

/** Called when a drawer has settled in a completely closed state. */
@Override
public void onDrawerClosed(View view) {
    super.onDrawerClosed(view);
    Log.i("foo", "drawer closing");
}

/** Called when a drawer has settled in a completely open state. */
@Override
public void onDrawerOpened(View drawerView) {
    super.onDrawerOpened(drawerView);
    Log.i("foo", "drawer opening");
}

and I found out that:

  • if I am in an activity and I click on the item for that activity in the drawer, the drawer will close

  • but if I go to another activity, the drawer will NOT close

I found this very strange, so I thought it maybe a timing issue. So I tried to wait for 1 second after calling closeDrawers() before doing anything else (i.e. changing activity):

// Set the list's click listener
mDrawerList.setOnItemClickListener(new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> arg0, final View v, final int position, long arg3) {

        // first just close the drawer
        mDrawerLayout.closeDrawers();

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = null;
                if (position == 0) { // Activity A
                    intent = new Intent(v.getContext(), ActivityA.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                }
                else if (position == 1) { // Activity B
                    intent = new Intent(v.getContext(), ActivityB.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                }
            }
        }, 1000);
    }
});

and sure enough, the drawer closed successfully.

My questions are:

  • Why is this happening? I thought they (closing drawer, changing activity) are all running on the same thread sequentially so one must finish before another can execute?
  • How can I make sure the navigation drawer closes before anything else is executed?

1条回答
Animai°情兽
2楼-- · 2019-05-11 01:30

as you mention it's about timing issue , the startActivity would be execute faster than closing the NavigationDrawer.about your second question you can set a DrawerListener for that and after your Navigation Closed completely start your second Activity :

mDrawerLayout.setDrawerListener(new DrawerListener() {

        @Override
        public void onDrawerStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDrawerSlide(View arg0, float arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDrawerOpened(View arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDrawerClosed(View arg0) {

            GotoActivity();
        }
    });
查看更多
登录 后发表回答