【问题标题】:Android MainActivity does not update fragment Actionbar title on onResume when back button is pressed按下后退按钮时,Android MainActivity 不会更新 onResume 上的片段操作栏标题
【发布时间】:2017-01-25 13:56:19
【问题描述】:

在导航抽屉中按下项目时。它创建了一个新片段。操作栏的标题更新。但是当我按下后退按钮时,上一个片段显示但 ActionBar 标题没有改变。 .这里是 DrawerItemClickListener 的代码。

private class DrawerItemClickListener implements ListView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
         mProgressBar.setVisibility(View.VISIBLE);
        selectItem(position);

    }
}


  private void selectItem(final int position) {

     mPendingRunnable = new Runnable() {
        @Override
        public void run() {
            FragmentManager fragmentManager = getSupportFragmentManager();
            switch (position) {
                case 0:
                    HomeFragment homeFragment = new HomeFragment();
                    fragmentManager.beginTransaction()
                            .replace(R.id.activity_home, homeFragment,"homeFragment")
                            .commit();
                       setTitle("Home");
                    break;
                case 1:
                    ProductFragment productFragment = new ProductFragment();
                    fragmentManager.beginTransaction()
                            .replace(R.id.activity_home, productFragment,"productFragment")
                            .addToBackStack(null)
                            .commit();
                            setTitle("Products");
                    break;

                case 2:
                    BillFragment billFragment = new BillFragment();
                    fragmentManager.beginTransaction()
                            .replace(R.id.activity_home, billFragment,"billFragment")
                            .addToBackStack(null)
                            .commit();
                             setTitle("Bill");
                    break;

            }
            mProgressBar.setVisibility(View.GONE);
        }
    };
mDrawerList.setItemChecked(position, true);
    mDrawerLayout.closeDrawer(mDrawerList);
}

我尝试使用以下代码在活动状态更改时更新标题。当按下后退按钮时,它总是将操作栏标题设置为 defaultTitle 。其他条件不执行。

   @Override
protected void onResume() {
    super.onResume();
    HomeFragment homeFragment = (HomeFragment)getSupportFragmentManager().findFragmentByTag("homeFragment");
    ProductFragment productFragment = (ProductFragment)getSupportFragmentManager().findFragmentByTag("productFragment");
    BillFragment billFragment = (BillFragment)getSupportFragmentManager().findFragmentByTag("billFragment");

    if(homeFragment != null && homeFragment.isVisible()) {
        setTitle(getString(R.string.Home));
    }
    else if( productFragment != null && productFragment.isVisible())
    {
        setTitle(getString(R.string.Products));
    }
    else if(billFragment != null && billFragment.isVisible()){
        setTitle(getString(R.string.Bills));
    }
    else {
        setTitle("defaultTitle");
    }
}

【问题讨论】:

    标签: android android-fragments android-actionbar


    【解决方案1】:

    不是在 Activity 的 onResume() 上设置标题,而是在片段中获取 ActionBar 实例并为每个片段设置标题

    在所有片段的onCreateView()onResume()中设置以下代码

    ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Your title");
    

    【讨论】:

    • 它有效。但它是一项复杂的工作。有没有办法在一个地方更新所有片段名称?为什么 findFragmentByTag 和 isVisible 不起作用?
    • 查找片段并设置它比上面的更复杂和昂贵。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多