yat0
yat0

Reputation: 967

Android: Title of the action bar disappears

so, in my main activity, on the onCreate() method, I check if it is the app first run with shared preferences... If it is the first run of the app, the user is redirected to a welcome activity, and then, when I press the back button and return to the main activity, the title in the action doesn't show up.. I have tested with api 9 and 17, and this only happens with api 9, so I'm guessing the error must be something about using the support library for the action bar .. Can someone help me ?

Main:

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;

public class MainActivity extends ActionBarActivity {

    SessionManager session;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // session
        session = new SessionManager(getApplicationContext());

        // check first time app run
        session.checkFirstRun();

    }

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

}

SessionManager First Run check method

// check first run 
    public void checkFirstRun() {
        if(getFirstRunStatus() == true) {
            // set first run key as false
            editor.putBoolean("FIRST_RUN", false);
            editor.commit();

            // first time running the app, redirect user to welcome activity
            Intent i = new Intent(_context, WelcomeActivity.class);
            // Closing all the Activities
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            // Add new Flag to start new Activity
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            _context.startActivity(i);
        }
    }

    public boolean getFirstRunStatus() {
        return pref.getBoolean(FIRST_RUN, true);
    }

Upvotes: 0

Views: 3934

Answers (2)

Emaster
Emaster

Reputation: 91

try this in your onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.action_menu_actions, menu);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle("title");
    actionBar .setDisplayShowTitleEnabled(true);
    // OR:
    // getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
    return true;
}

Upvotes: 1

jasonflaherty
jasonflaherty

Reputation: 1944

Add the title in a couple ways:

XML:

   <activity 
   android:name=".....WelcomeActivity" 
   android:icon="@drawable/logo" 
   android:label="@string/app_name"
   </activity>

On the Fly:

.setTitle("TITLE");
.setIcon(R.drawable.logo);

You can pass the title in an intent to if you want it to be dynamic... not sure if that is what you want:

.setTitle(extras.getString("title"));

Hope that helps.

Upvotes: 1

Related Questions