3

I'm trying to implement a Navigation Drawer in my app. All is good except when I switch to a new activity, it tries switching BEFORE the drawer animation is finished. This results in the animation getting very laggy and it looks pretty bad. Is there anyway I can do something like this

once drawer is closed --> start the new activity

Here is my code for my BaseActivity:

public class BaseActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
protected RelativeLayout _completeLayout, _activityLayout;
// nav drawer title
private CharSequence mDrawerTitle;

// used to store app title
private CharSequence mTitle;

private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawer);
    // if (savedInstanceState == null) {
    // // on first time display view for first nav item
    // // displayView(0);
    // }
}

public void set(String[] navMenuTitles, TypedArray navMenuIcons) {
    mTitle = mDrawerTitle = getTitle();

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

    navDrawerItems = new ArrayList<NavDrawerItem>();

    // adding nav drawer items
    if (navMenuIcons == null) {
        for (int i = 0; i < navMenuTitles.length; i++) {
            navDrawerItems.add(new NavDrawerItem(navMenuTitles[i]));
        }
    } else {
        for (int i = 0; i < navMenuTitles.length; i++) {
            navDrawerItems.add(new NavDrawerItem(navMenuTitles[i],
                    navMenuIcons.getResourceId(i, -1)));
        }
    }

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    // setting the nav drawer list adapter
    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    // enabling action bar app icon and behaving it as toggle button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    // getSupportActionBar().setIcon(R.drawable.ic_drawer);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, // nav menu toggle icon
            R.string.app_name, // nav drawer open - description for
            // accessibility
            R.string.app_name // nav drawer close - description for
    // accessibility
    ) {
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            supportInvalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            supportInvalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

}

private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // display view for selected nav drawer item
        displayView(position);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {
        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }
    }

    return super.onOptionsItemSelected(item);
}

/***
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    // boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    // menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}


private void displayView(int position) {
    switch (position) {
    case 0:
        // Intent intent = new Intent(this, First.class);
        // startActivity(intent);
        // finish();
        break;
    case 1:
        Intent intent1 = new Intent(this, SecondActivity.class);
        startActivity(intent1);
        finish();
        break;
    default:
        break;
    }

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}
}

Thanks.

user1282637
  • 1,827
  • 5
  • 27
  • 56

1 Answers1

1

The laggy effect comes only if you are testing on an emulator or an old device.try closing the drawer before the intent is called like this

 case 1:
        Intent intent1 = new Intent(this, SecondActivity.class);
        mDrawerLayout.closeDrawer(mDrawerList);
        startActivity(intent1);
        finish();
        break;
Aswin Rk
  • 31
  • 9
  • Thanks for the post. Although the logic is there, `mDrawerLayout.closeDrawer(mDrawerList);` only STARTS to close the drawer when the new activity is called. I tried this and it still is laggy/stuttering. I'm looking more for a way to ensure that it is closed before starting the new activity. – user1282637 Aug 20 '14 at 17:17
  • maynot be the perfect way to do this but try setting a delay after mDrawerLayout.closeDrawer(mDrawerList); and before starting the activity – Aswin Rk Aug 20 '14 at 17:21
  • I found some other people saying that. I may have to look deeper, though, I'm not a huge fan of putting in hardcoded delays, not exactly optimal. Thanks though1 – user1282637 Aug 20 '14 at 17:26
  • 1
    tried the onDrawerClosed function ? http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.DrawerListener.html#onDrawerClosed(android.view.View) – Aswin Rk Aug 20 '14 at 17:33
  • Yep, I actually already got it but I did accomplish it using onDrawerClosed(). If you update your answer I will accept it – user1282637 Aug 20 '14 at 18:01