繁体   English   中英

动作栏上按钮监听器

[英]Actionbar Up Button Listener

我的应用程序中只有一个活动,其中包含导航视图以浏览3个片段。

当我导航到片段时,我将汉堡菜单图标更改为onNavigationItemSelected方法中的向上按钮,如下所示:

actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

向上按钮看起来不错,但尚未生效。

当单击按钮时,我想导航到堆栈中的上一个片段,但无法为该按钮添加侦听器。

我试过使用:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        onBackPressed();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

但这从未被称为。 我认为未调用它是因为清单中没有父活动。

提前致谢。

您不应调用onBackPressed,而应使用片段事务处理方法在片段之间进行事务处理,就像我使用replace方法将当前片段替换为以前的片段一样。 容器是包含片段的布局。

  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
 transaction.replace(R.id.fragment_container, newFragment); 
     transaction.commit();  

希望能给您一些指导。

 if (id == android.R.id.home) {
    //put the code like above here.
    return true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM