0

I've created a NavigationDrawer Class using this answer: Same Navigation Drawer in different Activities

My problem is that when I click the navigation drawer item of the current activity, the activity reloads, I just need it to close the navigationDrawer if the clicked item is the same of the current activity.

This is my Drawer Activity

    import android.content.Intent;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.MenuItem;

    import com.rafael.test.LoginActivity;
    import com.rafael.test.HomeActivity;
    import com.rafael.test.R;
    import com.rafael.test.TokenActivity;

    public class DrawerBase extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener   {

        public DrawerLayout drawer;
public NavigationView navigationView;

protected void onCreateDrawer() {


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


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();


    // Creates the NavigationView and set Listener
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


}


@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

// Here is where I'm handling the menu item click
@Override
public boolean onNavigationItemSelected(MenuItem item) {


    int id = item.getItemId();

    if (id == R.id.home) {
        Intent i = new Intent(this, HomeActivity.class);
        startActivity(i);

    } else if (id == R.id.feed) {

        Intent i = new Intent(this,FeedActivity.class);
        startActivity(i);

    } else if (id == R.id.token) {

        Intent i = new Intent(this, TokenActivity.class);
        startActivity(i);

    }

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

Community
  • 1
  • 1
Rafael
  • 1,437
  • 6
  • 23
  • 41

2 Answers2

0

There are a few ways to do this.

You could make a conditional which simply checks the current activity, which could be stored in a static variable in BaseActivity.

Alternatively, if you never want to recreate an Activity that's on top, you could add singleTop launchMode to your manifest like so:

<activity
        android:name=".YourActivity"
        android:launchMode="singleTop"
</activity>

One other, similar alternative, is to add the singleTop flag to your navigationdrawer intent, if you don't want to recreate the activity from the nav drawer, but do want to recreate it in other places...

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Demonsoul
  • 791
  • 4
  • 11
0

Got the answer but for some reason the person who posted it, deleted it.

On my OnNavigationItemSelecte() method i changed the test to call the Intent

from this:

int id = item.getItemId();

    if (id == R.id.home) {
        Intent i = new Intent(this, HomeActivity.class);
        startActivity(i);

to this

if (id == R.id.home) {
    // This code gets the class name to use in the test
    if(this.getClass().getSimpleName().equals("HomeActivity")){
       drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    }else{
       Intent i = new Intent(this, HomeActivity.class);
        startActivity(i);
    }
Rafael
  • 1,437
  • 6
  • 23
  • 41