Reputation: 115
What I need: Create an options menu with "Sign in" option, when user signs in it needs to change to "Sign Out" when user taps on the menu button again
When using a pure SDK i can just change the menu options in onPrepareOptionsMenu the same works when using compatibility library v4 however when using ActionBarSherlock the menu won't update in onPrepareOptionsMenu; it still gets called, but the menu shown does not change.
Does anyone have a solution to this?
Upvotes: 2
Views: 1508
Reputation: 449
This thread is a bit old and I wasn't able to apply the suggested fix (or find a better one). I am using ActionBarSherlock 4.2 but I still have an issue when signing in on ICS devices where the options menu is an overlay: I couldn't figure out how to force invalidating the menu only AFTER the sign-in activity closes (and the app has determined the signed in state changed).
I resolved the issue with a flag to invalidate the menu onResume
. I feel like using a flag is a hack, but it's better than invalidating on every resume.
@Override
protected void onResume()
{
if ( checkLoginState == Boolean.TRUE )
{
invalidateOptionsMenu();
checkLoginState = Boolean.FALSE;
}
super.onResume();
}
Then in onOptionsItemSelected
I set the flag:
@Override
public boolean onOptionsItemSelected( MenuItem item )
{
// Handle item selection
switch ( item.getItemId() )
{
case R.id.actionbar_settings_signin_out:
if (isUserLoggedIn() == Boolean.TRUE )
{
logout();
validateOptionsMenu();
}
else
{
// set the flag so that on resume we update the options menu
checkLoginState = Boolean.TRUE;
startSignInActivity();
}
return true;
This is working for me but I'm curious to hear if others have solved it a better way.
Upvotes: 1
Reputation: 46
Yeah, this is a nasty bug in ABS. You can fix it like this: In FragmentActivity.java find onPrepareOptionsMenu(android.view.Menu menu) method and comment out or delete
if (mOptionsMenuInvalidated) {
line and it's corresponding closing bracket, so this block gets executed every time. Do the same in the FragmentMapActivity activity, if you are using it.
Upvotes: 3