Action item element handler in fragment
I have a simple app with two apps. The main activity populates the listFragment and the second activity populates the fragment with fields to add custom object (s) to the main activity.
In the second activity, I have a "save" icon in the action bar. I'm trying to figure out how to listen to this button by clicking on a fragment, so I can wrap up the textboxes and pass them back to the action via the interface.
I tried to override onOptionItemSelected
but it never hit. How would I handle this?
source to share
Ok, so the trick is in the fragments method onCreate
, you should call
setHasOptionsMenu(true);
then all you have to do is override onOptionsItemSelected
in the fragment and render the action bar by clicking there !!
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save : {
Log.i(TAG, "Save from fragment");
return true;
}
}
return super.onOptionsItemSelected(item);
}
source to share