سؤال

As per the Android guidelines, I implemented an ActionBarDrawerToggle that switches to the global context onDrawerOpened and a local context onDrawerClosed. This context switch involves changing the action bar items as well as the action bar title, and is really straightforward. The problem is, if I am navigating to a new screen then the action bar's title will change twice, once for the switch back to local context and again for the new screen that the user is navigating too. This seems clunky, and I can't seem to figure out a way to implement the title change such that the user doesn't see it twice.

هل كانت مفيدة؟

المحلول

It seems like the best way to handle this is to have a condition you check when the drawer closes.

Here's a short example:

private boolean mNoTitleChange;
private int mPosition = -1;

@Override
public void onDrawerClosed(View view) {
    if (mNoTitleChange) {
        startActivity(new Intent(CurrentActivity.this, NewActivity.class));
        mNoTitleChange = false;
        return;
    }
    getActionBar().setTitle(mTitle);
    invalidateOptionsMenu();
}

@Override
protected void onResume() {
    super.onResume();
    if (mPosition != -1) {
        setTitle(mYourTitles[mPosition]);
        mPosition = -1;
    }
}

Whenever you select an item in your DrawerLayout, adjust the boolean as needed.

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mNoTitleChange = true;
        mPosition = position;
        mDrawerLayout.closeDrawer(mDrawerList);
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top