开发者

Adding menus to child preference screens

I have a menu that comes up for my PreferenceActivity. In my child preference screens, I lose that menu (doesn't pop up). How can I make my menu pop up for children too?

Thanks.

Example:

<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:persisten开发者_如何转开发t="true">
  <PreferenceCategory
    android:title="some category"
    android:persistent="true"
    android:orderingFromXml="true">
    <PreferenceScreen
      android:title="some child screen"
      android:summary="some child summary">
      <PreferenceCategory
         ...

The first preference screen has the menu, but when you click on the child one, no menu. How can you add the menu?


I have better solution because Suriya's suffer from double showing of child's PreferenceScreen.

For child PreferenceScreen I use same PreferenceActivity. So at first for a child PreferenceScreen where we want to use option menu we set intent to our PreferenceActivity in onCreate method:

ourPreferenceScreen = (PreferenceScreen) findPreference("our_preference_screen");
if (ourPreferenceScreen != null) {
    Intent intent = new Intent(this, PreferenceActivity.class);
    intent.putExtra("ShowOurPreferenceScreen", true);
    ourPreferenceScreen.setIntent(intent);
}

Then later in a same onCreate method we detect if our PreferenceActivity is started with extra flag. If so we switch to wanted PreferenceScreen:

if (getIntent().getBooleanExtra("ShowOurPreferenceScreen", false)) {
    setPreferenceScreen(ourPreferenceScreen);
    return;
}

And at the end we hande onCreateOptionMenu and other related option menu methods like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (getPreferenceScreen().equals(ourPreferenceScreen)) {
        // your menu code
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}


I faced the similar issue. Here is what I did to overcome the issue.

In the preferenceActivity onCreate method,

        final PreferenceScreen childPref = (PreferenceScreen) findPreference("childPrefId");
        childPref .setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference)
            {
                Intent intent = new Intent(PreferenceActivity.this, YourSettings.class);
                intent.setAction("ShowChildPref");
                startActivity(intent);                    
                return true;
            }
        });

        Intent intent = getIntent();
        if(intent.getAction() != null && intent.getAction().equals("ShowChildPref"))
        {                
            setPreferenceScreen(childPref);

            /*Set Flags here based on intent what kind of menu to create in OnPrepareMenu.*/
        }
    }


You have to add the code for the menu in each class that might want to bring up a menu using the menu button. Just because the other activity is in the background does not mean the menu will launch. It needs to be set in each class that wil be forground and need the menu available.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜