detecting a click on action bar back button -(OnOptionsItemSelected not calling when click on action bar back button)
Put this on onCreateOptionsMenu method:
MenuItemCompat.setOnActionExpandListener(menu.findItem(R.id.action_search), new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//DO SOMETHING WHEN THE SEARCHVIEW IS CLOSING
return true;
}
});
You should add meta data your manifest.xml for which activity you want it
Like
<activity
android:name=".Example"
android:label="@string/Example"
android:theme="Theme.AppCompat.Light">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
and your code should be like this in Example
@Override
protected void onCreate(Bundle savedInstanceState) {
.......
getActionBar().setDisplayHomeAsUpEnabled(true);
......
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}