2016-05-19 22 views
1

我使用的導航抽屜和onBackPressed的方法不會運行時,我點擊工具欄的箭頭,但當我點擊後面的movil底部面板正在運行。 我不明白如何做同樣的事情,但使用箭頭按鈕。如何在點擊箭頭圖標時使用導航抽屜的Android中的onBackPressed?

公共類MainActivity擴展AppCompatActivity 實現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(); 

} 

}

+0

可能的[如何自定義ba ck按鈕在ActionBar](http://stackoverflow.com/questions/9252354/how-to-customize-the-back-button-on-actionbar) –

+0

你是從menu.xml膨脹它? – Mohit

回答

0

您可以更改主題

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

或者在onCreate()

getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_your_icon); 
+0

我嘗試這個,但所有時間它打開導航抽屜...... –

0

你可以試試這個:

toolbar.setNavigationOnClickListener(yourOnClickListener); 
1

首先使操作欄中的後退箭頭..

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

二使用android.R.id.home來訪問按鈕onOptionsItemSelected()

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()){   
     case android.R.id.home: 
      onBackPressed(); 
     break; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+0

我啓用接口onFragmentInteraction()上的後退箭頭並進行更改,但是當我單擊箭頭時不會調用onOptionsItemSelected。 我在沒有導航抽屜的其他項目中嘗試了這一點,並進行了更改。 –