jamis0n
jamis0n

Reputation: 3810

ActionBar Navigation List - Use same ActionBar in multiple activities; Initialize in one place

I'm finding myself rewriting the same code for my actionBar (actionBarSherlock) list in 3 separate activities. All 3 are using the same actionBar, which has 3 items which launch Activities #1, #2, #3.

getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ArrayAdapter<CharSequence> list = ArrayAdapter
    .createFromResource(this, R.array.action_list, android.R.layout.simple_dropdown_item_1line);
list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

getSupportActionBar().setListNavigationCallbacks(list, this);
getSupportActionBar().setListNavigationCallbacks(adapter, this);
getSupportActionBar().setSelectedNavigationItem(position);

I have 2 Questions:

  1. Should I be using 1 activity with 3 fragments in this case? My activities are a listview, a mapview, and a form view. I'm unsure of whether this is the right case to use Fragments, as each of those views uses the whole screen in my case.

  2. Using 3 different activities, can I create a new class whose sole purpose is to configure my ActionBar for me using the code above so the initialization code is only in 1 place?

Something like:

public class setupActionBar {
    private ActionBar myBar;
    public setupActionBar(ActionBar myBar){
        this.myBar = myBar;
        //Do Initialization on myBar;
    }

    public ActionBar getMyBar(){
        return myBar;
    }
}

Upvotes: 4

Views: 6908

Answers (3)

aindurti
aindurti

Reputation: 642

Watch this video. He creates a BaseActivity that subclasses all of the Activities used in the program. Neat little trick ;)

You can thank me later :D

Upvotes: 10

jamis0n
jamis0n

Reputation: 3810

Using the excellent tutorials suggested by @aindurti, I was able to get this working using a BaseActivity which Extends SherlockActivity, then my Activity1 which extends BaseActivity.

However, I'm experiencing weird behavior. When I select Activity #3 from the spinner, it immediately goes back to Activity#1, with onNavigationItemSelected being called 3 times. Thoughts on the code? Should I move onNavigationItemSelected into each Activity? I thought this would work as a concise and consolidated way.

In BaseActivity.java:

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    //Tell user the FROM and TO navigationIndex   
    Toast.makeText(getApplicationContext(), "Selected: " + itemPosition + " from" + currentNavigationIndex, Toast.LENGTH_SHORT).show();

    Intent myIntent;
    if(itemPosition != currentNavigationIndex){
        if(itemPosition == 0){ //Activity#1 Selected
            myIntent = new Intent(BaseActivity.this, Activity1.class);
        } else if (itemPosition == 1){ //Activity#2 Selected
            myIntent = new Intent(BaseActivity.this, Activity2.class);
        } else if (itemPosition == 2){ //Activity#3 Selected
            myIntent = new Intent(BaseActivity.this, Activity3.class);
        }
        BaseActivity.this.startActivity(myIntent);
    }
    return true;
}

Upvotes: 0

Joe Plante
Joe Plante

Reputation: 6368

There's several ways to do this. You can create a static function somewhere to do the initialization, or you can do something like create a base class for an activity.

For example:

public class Bob
{
   public static void dance(Dance someDance)
   {
      someDance.doBadDance();
   }
}

You can use that with Bob.dance(); anywhere. Not sure if the class has to be final or not

Upvotes: 0

Related Questions