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

时间:2017-01-25 13:56:19

标签: android android-fragments android-actionbar

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

1 个答案:

答案 0 :(得分:0)

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

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

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