Set a title in Toolbar from fragment in Android

后端 未结 17 802
逝去的感伤
逝去的感伤 2021-01-31 03:40

I have been using the latest Toolbar from AppCompatv7 lib.I have placed a textview in the ToolBar ViewGroup And I want to set a title into this Textview from the fragment in my

17条回答
  •  情歌与酒
    2021-01-31 04:17

    getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
                @Override
                public void onBackStackChanged() {
                    FragmentManager.BackStackEntry lastBackStackEntry=null;
    
                    int lastBackStackEntryCount = getSupportFragmentManager().getBackStackEntryCount() - 1;
                    if(lastBackStackEntryCount >= 0 )
                    {
                        lastBackStackEntry = getSupportFragmentManager().getBackStackEntryAt(lastBackStackEntryCount);
                    }
                    Toast.makeText(MainActivity.this, ""+lastBackStackEntryCount, Toast.LENGTH_SHORT).show();
                    if(lastBackStackEntryCount == -1)
                    {
                        toolbar.setTitle("");
                        toolbar.setLogo(R.drawable.header_logo);
                    }
                    else
                    {
                        toolbar.setTitle(lastBackStackEntry.getName());
                    }
                }
            });
    

提交回复
热议问题