Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
}
like image 483
jamis0n Avatar asked Dec 26 '22 15:12

jamis0n


1 Answers

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

like image 83
aindurti Avatar answered Jan 19 '23 00:01

aindurti