工具栏(SupportActionBar)标题在方向更改时更改为应用名称

Swa*_*ite 10 android android-appcompat android-fragments

我在我的活动中有这个代码:

protected void onCreate(Bundle savedInstanceState) {
    ...
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
      actionBar.setDisplayHomeAsUpEnabled(true);
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

我正在从以下各种片段更新ActionBar标题onResume():

ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
    actionBar.setTitle(title);
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,但在更改方向后,标题再次更改为应用程序名称.我怎么能克服这个?

编辑:经过调查更多,我试过这个,并发现这个奇怪的行为:添加此代码,我在片段中设置标题:

final ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
if (actionBar != null) {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.d("SWAPNIL", "IN RUN BEFORE: " + actionBar.getTitle());
            actionBar.setTitle(title);
            Log.d("SWAPNIL", "IN RUN AFTER : " + actionBar.getTitle());
        }
    }, 3000);
}
Run Code Online (Sandbox Code Playgroud)

这是日志:

10-13 10:27:04.526 3719-3719/com.example.xxxx D/SWAPNIL: onResumeHelp
10-13 10:27:07.528 3719-3719/com.example.xxxx D/SWAPNIL: IN RUN BEFORE: MY APP NAME
10-13 10:27:07.528 3719-3719/com.example.xxxx D/SWAPNIL: IN RUN AFTER : title
10-13 10:27:21.012 3719-3719/com.example.xxxx D/SWAPNIL: onResumeHelp
10-13 10:27:24.013 3719-3719/com.example.xxxx D/SWAPNIL: IN RUN BEFORE: title
10-13 10:27:24.013 3719-3719/com.example.xxxx D/SWAPNIL: IN RUN AFTER : title
Run Code Online (Sandbox Code Playgroud)

它按日志进行了更改,但没有在UI中反映出来.请帮我!

Swa*_*ite 9

好吧,最后,在这个愚蠢的事情上花了2天后,我得到了解决方案(我会说解决方法).

这可能是一个嵌套的Fragmentbug.

我有嵌套的Fragment结构.我们知道Fragment.getActivity()返回父母Activity.经过大量的调试后,我观察到如果你getActivity()在方向改变之后调用(甚至在里面Fragment.onActivityCreated()),它会返回旧的引用,Activity除了最顶层的父片段,它正确地返回新创建的Activity.

所以我写了这个方法来获取Activity来自任何方面的电流Fragment:

/**
 * When inside a nested fragment and Activity gets recreated due to reasons like orientation
 * change, {@link android.support.v4.app.Fragment#getActivity()} returns old Activity but the top
 * level parent fragment's {@link android.support.v4.app.Fragment#getActivity()} returns current,
 * recreated Activity. Hence use this method in nested fragments instead of
 * android.support.v4.app.Fragment#getActivity()
 *
 * @param fragment
 *  The current nested Fragment
 *
 * @return current Activity that fragment is hosted in
 */
public Activity getActivity(Fragment fragment) {
    if (fragment == null) {
        return null;
    }
    while (fragment.getParentFragment() != null) {
        fragment = fragment.getParentFragment();
    }
    return fragment.getActivity();
}
Run Code Online (Sandbox Code Playgroud)

  • 不确定我是否应该说得好或感谢.这对每个Android开发人员来说都是一个很好的教训 谷歌应该认真对待这些错误,而不仅仅是发布更多未完成的Android版本. (2认同)

Luk*_*son 5

您需要在此处查看 sorianiv 所写的答案: 在 android 应用程序中 Toolbar.setTitle 方法无效 – 应用程序名称显示为标题

添加附加内容toolbar.setTitle("")为我解决了这个问题。

    Toolbar toolbar = (Toolbar) root.findViewById(R.id.toolbar);
    toolbar.setTitle("");
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
    toolbar.setTitle("Profiles");
Run Code Online (Sandbox Code Playgroud)