kanytu / android-material-drawer-template

An Android template with navigation drawer for material design
Apache License 2.0
674 stars 217 forks source link

Drawer opens the included activities one by one. #8

Closed tekniksinc closed 9 years ago

tekniksinc commented 9 years ago

Thanks for the last answer regarding the opened drawer. I added a second activity, included the drawerlayout and changed the first's onNavigationDrawerItemSelected method to this:

@Override public void onNavigationDrawerItemSelected(int position) { switch(position){ case 0: Intent in = new Intent(getApplicationContext(),Activity1.class); startActivity(in); break; case 1: Intent in2 = new Intent(getApplicationContext(),Activity2.class); startActivity(in2); break; } }

The thing is that when i run the app, the drawer opens the main activity (activity1) again and again and it loops forever. I only want it to open when I press on it, not when it's selected (which by default is when you first run the app).

Any ideas? Thanks in advance for my idiotic questions.

kanytu commented 9 years ago

Create a boolean flag (class field) and set it it's default value to false. On onNavigationDrawerItemSelected start by checking if it's value is false. If so, change it's value to true and exit the function by calling return;.

Final result:

Create a boolean flag (class field) and set it it's default value to false. On onNavigationDrawerItemSelected start by checking if it's value is false. If so, change it's value to true and exit the function by calling return;.

Final result:

@Override
public void onNavigationDrawerItemSelected(int position) {
    if(!firstRun){
        firstRun = true;
        return;
    }
    switch (position) {
        case 0:
            Intent in = new Intent(getApplicationContext(), Activity1.class);
            startActivity( in );
            break;
        case 1:
            Intent in2 = new Intent(getApplicationContext(), Activity2.class);
            startActivity(in2);
            break;
    }
}

Keep in mind that this is not an "issue" of this library.