【问题标题】:How to use onBackPressed in Android of navigation drawer when click on the arrow icon?单击箭头图标时如何在导航抽屉的Android中使用onBackPressed?
【发布时间】:2016-05-19 08:09:01
【问题描述】:

我正在使用导航抽屉,当我点击工具栏的箭头时,onBackPressed 的方法没有运行,但是当我点击底部面板的背面时,movil 正在运行。 我不明白如何做同样的事情,但使用箭头按钮。

公共类 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();

}

}

【问题讨论】:

标签: android fragment navigation-drawer back


【解决方案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);
}

【讨论】:

  • 我在 onFragmentInteraction() 接口上启用了后退箭头并进行了更改,但是当我单击箭头时不会调用 onOptionsItemSelected。我在没有导航抽屉的其他项目中尝试了这个并进行了更改。
【解决方案2】:

你可以改变主题

<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);

【讨论】:

    【解决方案3】:

    你可以试试这个:

    toolbar.setNavigationOnClickListener(yourOnClickListener);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多