Android 替换片段后如何更改此标题?

Android 替换片段后如何更改此标题?,android,Android,这是我的HomeFragment.java: 此外,GalleryFragment.java是 以下是我的MainActivity.java: 任何反馈都将不胜感激。 我不熟悉碎片 它要求我补充更多细节 这里有更多的细节 这里有更多的细节 通过片段,onResume方法,您可以更改工具栏标题,如下所示 @Override public void onResume() { super.onResume(); // Set title ((MainActivity) getA

这是我的HomeFragment.java:

此外,GalleryFragment.java是

以下是我的MainActivity.java:

任何反馈都将不胜感激。 我不熟悉碎片 它要求我补充更多细节 这里有更多的细节 这里有更多的细节


通过
片段
onResume
方法,您可以更改工具栏标题,如下所示

@Override
public void onResume() {
    super.onResume();
    // Set title
    ((MainActivity) getActivity()).getSupportActionBar().setTitle("Your Title");
}

但当我按下后退按钮时,HomeFragment中也有相同的标题。如何修复?更新后请检查一次。并接受答案。干杯
public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.fragment_gallery, container, false);
    getActivity().setTitle("Team B");
    gallery_recycler_view= root.findViewById(R.id.gallery_recycler_view);
}
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send,R.id.nav_notifications)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}
@Override
public void onResume() {
    super.onResume();
    // Set title
    ((MainActivity) getActivity()).getSupportActionBar().setTitle("Your Title");
}