6

I am following this post http://derekrwoods.com/2013/09/creating-a-static-navigation-drawer-in-android/

I want drawer to be opened when in landscape mode. Here is my onCreate function.

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_order);

    drawerList = (ListView) findViewById(R.id.left_drawer);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    FrameLayout frameLayout = (FrameLayout) findViewById(R.id.content_frame);

    if (((ViewGroup.MarginLayoutParams) frameLayout.getLayoutParams()).leftMargin == (int) getResources()
            .getDimension(R.dimen.drawer_size)) {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
        drawerLayout.setScrimColor(Color.TRANSPARENT);
        isDrawerLocked = true;
    }

    // Set the adapter for the list view
    // drawerItems = getResources().getStringArray(R.array.drawerOptions);
    drawerItems = DummyContent.ITEMS
            .toArray(new DummyItem[DummyContent.ITEMS.size()]);
    drawerList.setAdapter(new ArrayAdapter<DummyItem>(this,
            android.R.layout.simple_list_item_1, drawerItems));
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
            R.drawable.ic_drawer, R.string.action_short,
            R.string.action_short) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            getActionBar().setTitle("test");
            // ((FragmentInterface) fragment).showMenuActions();
            invalidateOptionsMenu();
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle("Select Option");
            // ((FragmentInterface) fragment).hideMenuActions();
            invalidateOptionsMenu();
        }
    };

    if (!isDrawerLocked) {
        drawerLayout.setDrawerListener(drawerToggle);
    }

    // Set the drawer toggle as the DrawerListener
    DrawerItemClickListener drawerItemClickListener = new DrawerItemClickListener();
    drawerList.setOnItemClickListener(drawerItemClickListener);

    if (!isDrawerLocked) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

Now the problem is whenever my activity is starting in portrait mode, after rotation drawer is collapsible (can be closed using sliding right to left gesture) even after setting DrawerLayout.LOCK_MODE_LOCKED_OPEN. This problem does not occur when opened directly in Landscape mode.

Apurv Gupta
  • 870
  • 7
  • 14

2 Answers2

6

I had to move that code to my onResume():

@Override
public void onResume() {
    super.onResume();

    Display display = getWindowManager().getDefaultDisplay();

    if (display.getRotation() == Surface.ROTATION_90 || display.getRotation() == Surface.ROTATION_270) {
        // Landscape
        isDrawerLocked = true;
         mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
    } else {
        // Portrait
        isDrawerLocked = false;
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    }
}
elmango
  • 429
  • 8
  • 19
  • 1
    This fixed a similar problem I was having, even though the code was being called. Moving it into onResume made the lock/unlock work correctly. If anyone knows why that's the case I'd love to know. – Ben987654 Mar 08 '16 at 00:55
-1

You should handle configuration changes yourself.

Use the onConfigurationChanged callback method to always know which orientation you're in. Make sure to include the android:configurationChanges="orientation" tag in your manifest. See this section of the user guide for a quick description of handling configuration changes.

I think the root of the issue is that Android is handling life cycle methods slightly differently than you're expecting. Using the explicit onConfigurationChanged method will remove any ambiguity.

Collin Flynn
  • 751
  • 6
  • 8
  • What values should I reinitialize on onConfigChanges?? – Apurv Gupta Dec 24 '13 at 06:48
  • In onConfigurationChanged, check the new orientation. If it's landscape, open the drawer and lock it. If it's portrait, set the drawer back to closed and unlock it, or set it back to whatever the user had last done in portrait. – Collin Flynn Dec 24 '13 at 14:36