Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar (SupportActionBar) title changes to app name on orientation change

I have this code in my Activity:

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

I'm updating the ActionBar title from various fragments like this in onResume():

ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
    actionBar.setTitle(title);
}

This is working fine, but after orientation change, the title changes to the app name again. How I can overcome this?

EDIT: After investigating more, I tried this and found this weird behaviour: Added this code where I was setting title in Fragments:

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

And here's the log:

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

It was getting changed as per logs but wasn't reflected in UI. Please help me!

like image 368
Swapnil Bhoite Avatar asked Oct 13 '15 12:10

Swapnil Bhoite


People also ask

How do I change the title text bar in Android?

First, add a font file in the src/main/assets/fonts/ of your project. Then create variables for Toolbar and text title and call the method findViewById(). Create a new Typeface from the specified font data. And at last setTypeface in text title.

How do I change the color of my action bar title?

xml in values folder this will change your action bar color.. Replace #666666 with your selected color code for title background color and replace #000000 for your title text color.


3 Answers

Okay, finally, after spending 2 days on this silly thing, I got the solution (I would say workaround).

This is probably a nested Fragment bug.

I have nested Fragment structure. As we know Fragment.getActivity() returns parent Activity. After lot of debugging I observed that if you call getActivity() after orientation change (even inside Fragment.onActivityCreated()) it returns reference of the old Activity except in top most parent fragment where it correctly returns the newly created Activity.

So I've written this method to get current Activity from any 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();
}
like image 134
Swapnil Bhoite Avatar answered Nov 14 '22 12:11

Swapnil Bhoite


You need to see the answer written by sorianiv here: In android app Toolbar.setTitle method has no effect – application name is shown as title

Adding the additional toolbar.setTitle("") resolved this issue for me.

    Toolbar toolbar = (Toolbar) root.findViewById(R.id.toolbar);
    toolbar.setTitle("");
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
    toolbar.setTitle("Profiles");
like image 33
Luke Allison Avatar answered Nov 14 '22 11:11

Luke Allison


I just ran into the same issue, and I solved it by using getSupportActionBar instead of toolbar.

getSupportActionBar().setTitle("...");
like image 40
alekop Avatar answered Nov 14 '22 11:11

alekop