Spike
Spike

Reputation: 147

Hiding some action items in ActionBarSherlock in some activities

I am not getting how to show only selected action items on some activities. Below is my code:

  public abstract class BaseActivity extends SherlockActivity { 


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            menu.add("Login")
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

            menu.add("Save")
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);


            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

            switch(item.getItemId())
            {


            case 0:
                Intent loginIntent = new Intent(BaseActivity.this,LoginForm.class);
                startActivity(loginIntent);
                return true;

                case 1:
                Intent saveIntent = new Intent(BaseActivity.this,SaveForm.class);
                startActivity(saveIntent);
                return true;
            }
            return false;
        }

    }

In the above code I am setting action items so that I can reuse this action items in some activities without repeating the code. Whenever I want these action items to be displayed on particular activity, I am extending this BaseActivity on the present activity as below.

public class FirstActivity extends BaseActivity {   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    } 
....
....
}

So, whenever I need these action items on the particular activities, I am extending the baseactivity to the activities on which I need. Now my problem is how to set only selected action items on particular activity. Suppose I don't want to show up "login" action Item on one activity. How can I get this done? Code snippets would be appreciated.

Upvotes: 0

Views: 911

Answers (1)

Warpzit
Warpzit

Reputation: 28152

Now it's a reasonable approach but I would only have items that were reused for all in the baseactivity. That said I think the correct way of handling this would be to override the methods with the implementation you want in these other activities.

You could have some variables that is set in onCreate(), if these are true/false handle it accordingly in onCreateOptionsMenu()

Edit

To override in your own activity you simply do the same thing as in the baseactivity but without calling initializing those buttons you don't need.

If you instead want to make toggle if buttons should be there or not simply add the variables to your baseactivity:

protected boolean mIsLoginButton = true;

Change the onCreateOptionsMenu() method in baseactivity to something like this:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if(mIsLoginButton) {
            menu.add("Login")
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }
        menu.add("Save")
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);


        return true;
    }

Then in your firstActivity you simply do this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mIsLoginButton = false;
} 

Upvotes: 1

Related Questions