简体   繁体   中英

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

When an item is pressed in navigation drawer . It creates a new fragment . The title of the actionbar updates. But when i press the back button previous fragment displays But ActionBar title does not changes . .Here is the code for 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);
}

I tried the below code to update the title when activity state changes. When back button pressed it always set actionbar Title as defaultTitle . Other conditions does not executed.

   @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");
    }
}

Instead of setting title on Activity's onResume() , get the ActionBar instance in fragment and set title there for every fragment

Set the below code in onCreateView() or onResume() of all fragments

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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