0

So I have a Search Button in my Action Bar, But I only want that button in the first Activity, but when I open the seconde activity, the Action button is still there. How to get rid of that button in the seconde activity without removing the action bar, because I nee it from my Navigation Drawer.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...
</menu>

Activity1:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

Activity2:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //I thought removing the INFLATER part would help, but it didn't...
    return super.onCreateOptionsMenu(menu);
}
Tha.Poox
  • 53
  • 1
  • 1
  • 4

3 Answers3

0

You can make the Item in your 2nd Activity invisible like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem item = menu.findItem(R.id.action_search);      
    item.setVisible(false);  
    return super.onCreateOptionsMenu(menu);
}

Another approach would be to load a second menu.xml with no item in it.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • Thank you for your answer, it is the same as singularhum's answers :) Just for information, I've already tried the seconde menu.xml with no item in it, it didn't work. Thanks again for the help – Tha.Poox Apr 13 '14 at 20:42
  • NP, consider to accept an answer to indicate that your problem is solved. – Steve Benett Apr 13 '14 at 20:44
  • did you post first? I can't tell but I can remove mine if you want since it's the same. – singularhum Apr 13 '14 at 20:45
0

You can try hiding the search menu item in your second activity.

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.findItem(R.id.action_search).setVisible(false);
    return true;
}

You can refer to this question with multiple answers.

Community
  • 1
  • 1
singularhum
  • 5,072
  • 2
  • 24
  • 32
0

if you return false it will not be shown. as android docment saying http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu%28android.view.Menu%29

@Override
public boolean onCreateOptionsMenu(Menu menu) {
     return false;
}
Fahid
  • 225
  • 1
  • 2
  • 8