Android 当按下后退按钮时,MainActivity 不会更新 onResume 上的片段 Actionbar 标题

Android MainActivity does not update fragment Actionbar title on onResume when back button is pressed

在导航抽屉中按下某个项目时。它创建一个新的片段。操作栏的标题更新。但是当我按下后退按钮时,前一个片段显示但 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);
}

我尝试使用以下代码在 activity 状态更改时更新标题。当按下后退按钮时,它总是将操作栏标题设置为 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");
    }
}

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

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

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