ActionBar action list - use the same ActionBar in several actions; Initialize in one place

I find that I am rewriting the same code for my actionBar list (actionBarSherlock) in 3 separate actions. All 3 use the same actionBar, which has 3 elements that trigger actions # 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:

  • Should I use 1 activity with 3 fragments in this case? My actions are a list, a map view, and a form view. I am not sure if it is correct to use Fragments, since each of these views uses the whole screen in my case.

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

Sort of:

 public class setupActionBar { private ActionBar myBar; public setupActionBar(ActionBar myBar){ this.myBar = myBar; //Do Initialization on myBar; } public ActionBar getMyBar(){ return myBar; } } 
+4
source share
3 answers

Watch this video . It creates a BaseActivity that subclasses all the actions used in the program. Neat little trick;)

You can thank me later: D

+10
source

There are 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 the activity.

For instance:

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

You can use this with Bob.dance (); anywhere. Not sure if class should be final or not

0
source

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

However, I experience strange behavior. When I select Activity # 3 from the counter, it immediately returns to Activity # 1, with onNavigationItemSelected called 3 times. Thoughts on the code? Should I move onNavigationItemSelected to every action? I thought this would be 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; } 
0
source

Source: https://habr.com/ru/post/1444835/


All Articles