返回上一个Activity时NavDrawer的不需要的动画

时间:2015-06-12 08:01:38

标签: android performance navigation navigation-drawer

我有一个导航抽屉,可在选择项目时启动活动。这就是问题所在:当我从一个活动中按回来时,我回到上一个活动,但不幸的是我看到导航抽屉关闭的动画。 这是一个不受欢迎的行为:我想回去看看根本没有动画!

这是我的代码:

private DrawerLayout mDrawerLayout; //declared globally in my Activity

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); //located in my OnCreate()

这是我回到上一个活动时关闭NavDrawer的方法:

@Override
public void onResume(){
    mDrawerLayout.closeDrawer(Gravity.START);

    super.onResume();
}

错误在哪里?

修改

以下是我如何从导航抽屉开始活动:

@Override
        public void onClick(View view, int position) {


            switch(position){

                case 0 : {
                            Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), PreparativiActivity.class);
                            NavigationDrawerFragment.this.startActivity(myIntent);
                            break;
                         }


                case 1 : {
                            Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), MainActivity.class);
                            NavigationDrawerFragment.this.startActivity(myIntent);
                            break;
                         }


                case 2 : {
                            Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), ModulisticaActivity.class);
                            NavigationDrawerFragment.this.startActivity(myIntent);
                            break;
                         }




                case 3 : {
                            Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), PoloActivity.class);
                            NavigationDrawerFragment.this.startActivity(myIntent);
                            break;
                         }


                case 4 : {
                          Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), ContattiActivity.class);
                          NavigationDrawerFragment.this.startActivity(myIntent);
                          break;
                         }


                case 5 : {
                          Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), IntroQuizActivity.class);
                          NavigationDrawerFragment.this.startActivity(myIntent);
                          break;
                         }


                case 6 : {
                           Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), CreditiActivity.class);
                           NavigationDrawerFragment.this.startActivity(myIntent);
                           break;
                          }


                default: { Toast.makeText(getActivity(), "Errore di sistema numero 001 ", Toast.LENGTH_SHORT).show();

                         }




            }

        }

1 个答案:

答案 0 :(得分:0)

恢复活动时不应关闭抽屉:

@Override
public void onResume(){
mDrawerLayout.closeDrawer(Gravity.START); //this should have been done before
super.onResume();
}

相反,您应该从抽屉中关闭它时启动活动

@Override
public void onClick(View view, int position) {
        switch(position){

            case 0 : {
                        Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), PreparativiActivity.class);
                        mDrawerLayout.closeDrawer(Gravity.START);
                        NavigationDrawerFragment.this.startActivity(myIntent);
                        break;
                     }
//And so on...

另一种可能性(隐藏关闭动画甚至更多)是在开始新活动后关闭抽屉:

@Override
    public void onClick(View view, int position) {


        switch(position){

            case 0 : {
                        Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), PreparativiActivity.class);
                        NavigationDrawerFragment.this.startActivity(myIntent);
                        break;
                     }


            case 1 : {
                        Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), MainActivity.class);
                        NavigationDrawerFragment.this.startActivity(myIntent);
                        break;
                     }


            case 2 : {
                        Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), ModulisticaActivity.class);
                        NavigationDrawerFragment.this.startActivity(myIntent);
                        break;
                     }




            case 3 : {
                        Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), PoloActivity.class);
                        NavigationDrawerFragment.this.startActivity(myIntent);
                        break;
                     }


            case 4 : {
                      Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), ContattiActivity.class);
                      NavigationDrawerFragment.this.startActivity(myIntent);
                      break;
                     }


            case 5 : {
                      Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), IntroQuizActivity.class);
                      NavigationDrawerFragment.this.startActivity(myIntent);
                      break;
                     }


            case 6 : {
                       Intent myIntent = new Intent(NavigationDrawerFragment.this.getActivity(), CreditiActivity.class);
                       NavigationDrawerFragment.this.startActivity(myIntent);
                       break;
                      }


            default: { Toast.makeText(getActivity(), "Errore di sistema numero 001 ", Toast.LENGTH_SHORT).show();

                     }
            } //switch end
            mDrawerLayout.closeDrawer(Gravity.START);
}//onClick end

编辑: 这个解决方案的一个问题可能是结束动画似乎有点滞后。这是因为Android系统已经忙于渲染新的Activity,因此没有足够的容量来渲染结束动画。

解决此问题,您可以加快动画(因此动画渲染会更少)。不幸的是,这样做似乎有点复杂。

要了解有关加快动画效果的更多信息,请阅读: Speed up 'Navigation Drawer' animation speed on closing?