简体   繁体   English

Android Navigation Drawer错误的工具栏标题onResume

[英]Android Navigation Drawer wrong toolbar title onResume

I have a Navigation Drawer in the main activity of my app. 我的应用程序的主要活动中有一个导航抽屉。 On the onCreate method of the activity I initialize one of the fragments like this : 在活动的onCreate方法中,我初始化了以下片段之一:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        MenuItem menuItem = navigationView.getMenu().findItem(R.id.menu_history);
        openFragment(menuItem);
    }

public void openFragment(MenuItem menuItem){
    Fragment newFragment = null;

    switch (menuItem.getItemId()){
        case R.id.menu_history :
            newFragment = new HistoryFragment();
            break;
        //.....
    }

    if (newFragment != null){
        //Replace content frame in activity_main.xml with newFragment
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, newFragment)
                .commit();

        menuItem.setChecked(true);
        getSupportActionBar().setTitle(menuItem.getTitle());
    }

    drawerLayout.closeDrawers();
}

This all works well, the fragment appears on startup with the title on the toolbar being "History". 这一切都很好,片段会在启动时出现,工具栏上的标题为“历史记录”。 But when the app goes into onPause and then onResume the toolbar title switches from "History" to the app name. 但是,当应用程序进入onPause然后进入onResume时,工具栏标题将从“历史记录”切换为应用程序名称。 I suspect this is an issue with onResume not opening the fragment / returning to its previous state correctly, because when I added the following lines to onResume the issue stopped: 我怀疑这是onResume无法打开片段/无法正确返回其先前状态的问题,因为当我向onResume添加以下行时,该问题就停止了:

@Override
protected void onResume() {
    super.onResume();
    MenuItem menuItem = navigationView.getMenu().findItem(R.id.menu_history);
    openFragment(menuItem);
}

That solution seems to fix it but that means it has to reload the fragment with the animations every time the app is resumed, which isn't optimal. 该解决方案似乎可以解决此问题,但这意味着它必须在每次恢复应用程序时重新加载动画片段,这并不是最佳选择。 Any ideas on how to fix this? 有想法该怎么解决这个吗?

If it helps, to recreate the issue I found useful to make the app split screen, because it always calls onResume. 如果有帮助,请重新创建该问题,我发现该问题对于使应用拆分屏幕很有用,因为它始终调用onResume。 Thanks. 谢谢。

Inside your openFragment() method, you write: openFragment()方法中,您编写:

    getSupportActionBar().setTitle(menuItem.getTitle());

This is the only thing that changes your toolbar's title. 这是唯一更改工具栏标题的方法。

Note that this code has nothing to do with your Fragment s, per se. 请注意,这段代码本身与Fragment无关。 When your activity is destroyed and recreated, the active Fragment will be successfully (automatically) destroyed and recreated by the FragmentManager ... but your openFragment() method won't run again and so nothing will update your toolbar's title. 当您的活动被破坏并重新创建,主动Fragment会成功(自动)破坏并通过重新FragmentManager ...但你openFragment()方法将不再运行,因此什么都不会更新工具栏的标题。

There are numerous ways you could solve this. 您可以通过多种方式解决此问题。 Probably the right thing to do is update your toolbar's title from within one of your Fragment 's lifecycle methods. 可能正确的做法是从Fragment的生命周期方法之一中更新工具栏的标题。

Edit: A reasonable place to update your toolbar's title would be in your fragment's onActivityCreated() method. 编辑:更新工具栏标题的合理位置将在您片段的onActivityCreated()方法中。 This will run both when the fragment is first added and during recreation. 这将在首次添加片段时以及重新创建过程中运行。 Something like: 就像是:

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("hello world");
}

如果您不想每次都重新创建该片段,则应使用如下所述的saveInstanceState: https ://stackoverflow.com/a/17135346/1505074

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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