Reputation: 117
my problem is when i click on a action bar item onOptionsItemSelected is called but not working.
I only want the back button to work. Here is my code, this is in the SherlockFragment file:
actionbar = getSherlockActivity().getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.menu_detail, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.detailitem1:
break;
case android.R.id.home:
getActivity().onBackPressed();
break;
default:
return super.onOptionsItemSelected(item);
}
return false;
}
The menu item is com.actionbarsherlock.view.MenuItem. I added setHasOptionsMenu(true); in onCreate and tried onCreateView too.
The onBackPressed is this, its in the SherlockFragmentActivity:
@Override
public void onBackPressed() {
Saved = SP.getBoolean("saved", false);
if ((getSupportFragmentManager().findFragmentByTag("detail") == null)
| mTwoPane) {
ready = true;
} else
ready = false;
if (!Saved && ready) {
new AlertDialog.Builder(this)
.....
}else if (Saved && ready){
super.onBackPressed();
}else {
someListFragment Fragment = new someListFragment ();
Fragment .setArguments(getIntent().getExtras());
Fragment .setHasOptionsMenu(true);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, Fragment, "listview")
.commit();
}
}
In the onbackpressed the ready is true when we are in two pane mode, so when using tablet, or if we are in the first fragment(so the second is null).
If i press the back button everything works fine, the fragments are replaced. But when i click on the back button in the action bar it just goes back to a previous activity.
Even if i chage the getActivity().onBackPressed(); onBackPressed's else statement i doesnt work.
Does anyone know whats the solution, what i did wrong?
Upvotes: 0
Views: 257
Reputation: 117
Sorry i was a bit lazy, i forget that i had a onOptionsItemSelected in my FragmantActivity, and the back button is android.R.id.home everwhere, so i put an if statement there and voila.
Upvotes: 0