AlexCheuk
AlexCheuk

Reputation: 5845

ActionBarSherlock: Change dropdown navigation title

currently i am using ActionBarSherlock for my project. I am creating my actionbar with this code.

setTheme(R.style.Theme_Sherlock);

Context context = getSupportActionBar().getThemedContext();
list = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this);

My question is. After I choose an option from the dropdown navigation, How do I keep that state throughout my activities?

For Example, in the homescreen, I choose "Sports" under my dropdown navigation. The title of the dropdown navigation then becomes "Sports". When I change activites however, the dropdown navigation title defaults back to the first item on the list.

Upvotes: 2

Views: 2131

Answers (1)

PrplRugby
PrplRugby

Reputation: 631

One method I used was to create a base activity that each navigation item / activity extended from. Within the base activity, I overloaded onResume with an int to track which activity was active, and set the selected navigation item in that method.

Example:

public class BaseActivity extends FragmentActivity {

    //...

    protected void onResume(final int actId) {
        super.onResume();

        //...setup your action bar via getSupportActionBar() calls...
        getSupportActionBar().setSelectedNavigationItem(actId);
}

Then in your individual activities:

public class ExampleActivity extends BaseActivity {
    private final int ACT_ID = 1;

    //...

    protected void onResume() {
        super.onResume(ACT_ID);

        //...
    }
}

Hope that helps!

Upvotes: 7

Related Questions