简体   繁体   中英

Android Activity action bar label not being set properly?

I have an app I'm designing where I have an activity with several buttons in it. This activity uses "setTitle()" to set the action bar title to the name of the user, where the default was set to a blank string in the AndroidManifest via android:label. One of the buttons leads to another activity that has a tab layout with fragments with different pieces of data. I use android:label in the manifest to set the title of this tab layout activity to a raw string. However, when entering this activity, it shows the user's name which was in the title of the activity that launched it. Why is this? Is there something wrong with how I'm defining the label of the activities? Here is my AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="edu.uml.android.adventurersarchive">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Preferences"
                  android:parentActivityName=".MainActivity"
                  android:theme="@android:style/Theme.Holo" />
        <activity android:name=".CreateCharacterActivity"
                  android:parentActivityName=".MainActivity"
                  android:label="Create a Character" />
        <activity android:name=".CharacterMainActivity"
                  android:parentActivityName=".MainActivity"
                  android:label="" />
        <activity android:name=".CharacterSheetActivity"
                  android:parentActivityName=".CharacterMainActivity"
                  android:label="" />
        <activity android:name=".CharacterEquipmentActivity"
                  android:parentActivityName=".CharacterMainActivity"
                  android:label="Equipment" />
        <activity android:name=".CharacterSpellbookActivity"
                  android:parentActivityName=".CharacterMainActivity"
                  android:label="Spellbook" />
    </application>
</manifest>

The activity in question which is displaying the wrong title is "CharacterSpellbookActivity". The one that sets the title to the user name is "CharacterMainActivity".

Here is the onCreate() method of CharacterMainActivity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_character_main);

    Intent intent = getIntent();

    myCharacter = (CharacterInfo) intent.getParcelableExtra("character");
    setTitle(myCharacter.getCharacterName());
}

Here is where I launch the CharacterSpellbookActivity:

public void openSpellbook(View v) {
    Intent intent = new Intent(this, CharacterSpellbookActivity.class);
    intent.putExtra("character", myCharacter);
    startActivity(intent);
}

And here is the onCreate() method of the CharacterSpellbookActivity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_character_spellbook);

    Intent intent = getIntent();

    myCharacter = (CharacterInfo) intent.getParcelableExtra("character");
    setTitle(myCharacter.getCharacterName());

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    SpellbookTabAdapter adapter = new SpellbookTabAdapter(myCharacter, getSupportFragmentManager());

    viewPager.setAdapter(adapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

Did I do something wrong here?

However, when entering this activity, it shows the user's name which was in the title of the activity that launched it. Why is this?

Because that's what you told it to do

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_character_spellbook);

    Intent intent = getIntent();

    myCharacter = (CharacterInfo) intent.getParcelableExtra("character");
    setTitle(myCharacter.getCharacterName());

Empty labels automatically picks up their value if they are null from their parent Activity.

Lets solve this :

remove the label attribute from manifest

If you want to change only the title why don't you ease down on your code and try this:

1

getActionBar().setTitle(R.string.your_title);

After it, you can call:

2

getActionBar().setDisplayShowTitleEnabled(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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