1

I'm using the navigation drawer and the method of onBackPressed don't run when I click on the arrow of toolbar, but when I click on back of the bottom panel of the movil is running. I don't understand how to do the same but using the arrow button.

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, BlankFragment.OnFragmentInteractionListener {

ActionBarDrawerToggle toggle;
DrawerLayout drawer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

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

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

    drawer.setDrawerListener(toggle);

    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.vista, new BlankFragment())
            .addToBackStack(null)
            .commit();

}


@Override
public void onBackPressed() {
    super.onBackPressed();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    if (id == android.R.id.home) {
        onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {

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

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

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

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

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

    }

    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onFragmentInteraction() {

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.vista, new BlankFragment2())
            .addToBackStack(null)
            .commit();

}

}

Eric Retamero
  • 29
  • 1
  • 7

3 Answers3

1

First enable the back arrow of action bar..

ActionBar actionBar = getSupportActionBar();
if(actionBar!=null){
    actionBar.setDisplayHomeAsUpEnabled(true);
}

Second use android.R.id.home to access to button on onOptionsItemSelected()

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()){           
       case android.R.id.home:
           onBackPressed();
         break;
    }
    return super.onOptionsItemSelected(item);
}
Iamat8
  • 3,888
  • 9
  • 25
  • 35
  • I enable the back arrow on the interface onFragmentInteraction() and makes the changes but when I click on the arrow does not call the onOptionsItemSelected. I tried this in other project without navigation drawer and makes the changes. – Eric Retamero May 19 '16 at 09:53
0

You can change theme

<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
    <item name="android:homeAsUpIndicator">@drawable/ic_your_icon</item>
</style>

Or in onCreate()

getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_your_icon);
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
0

You can try this:

toolbar.setNavigationOnClickListener(yourOnClickListener);
antonicg
  • 934
  • 10
  • 24