Question

I create base activity with actionbar menu functional and extended another activities from it.

public class BaseActivity extends SherlockFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        ...
        return true;
    }


}

Now I want to add some buttons to the actionbar in some activities. How can i add elements to actionbar and use element from base activity?

Was it helpful?

Solution

You can do that as simple as in BaseActivity just don't forget to call super.onCreateOptionsMenu().

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:id="@+id/action_help"
        android:icon="@drawable/ic_action_help"
        android:title="@string/action_help" />
</menu>

home.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:id="@+id/action_new"
        android:icon="@drawable/ic_action_new"
        android:title="@string/action_new" />
</menu>

In BaseActivity you are using base menu config main.xml.

public class BaseActivity extends SherlockFragmentActivity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

In child activity you are using another config from home.xml only with additional menu items, no duplicates. But don't forget to call super.onCreateOptionsMenu(menu) with the same menu instance to add elements from BaseActivity (parent menu items would be added after child menu items if you call super's method after inflate, and before otherwise).

public class HomeActivity extends BaseActivity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.home, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

OTHER TIPS

You can specify your actions for the actionbar in the main.xml

example :

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"/>
<item android:id="@+id/action_compose"
      android:icon="@drawable/ic_action_compose"
      android:title="@string/action_compose" />

And call from your subclass

@Override
public boolean onOptionsItemSelected(MenuItem item) {       
    return super.onOptionsItemSelected(item);
}

Make different xml files containing the buttons you need specific to those Activity. Say for Activity1 you have two buttons and for Activity2 you have one, then you will create 2 xml files like below.

action_activity1.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/action_search"/>
   <item android:id="@+id/action_compose"
    android:icon="@drawable/ic_action_compose"
    android:title="@string/action_compose" />
</menu>

action_activity2.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/action_search"/>
</menu>

Then in the onCreateOptionsMenu(Menu menu) method inflate the desired xml file. Like,

Activity1:

public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate the menu; this adds items to the action bar if it is present.
   getMenuInflater().inflate(R.menu.action_activity1, menu);
   return true;
}

Activity2:

public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate the menu; this adds items to the action bar if it is present.
   getMenuInflater().inflate(R.menu.action_activity2, menu);
   return true;
}

Please be care full with the syntax (as I used appcompat actionbar). :)

This is what I use. Hope you find it helpful. And, I will be happy to see an easier way than this. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top