onPrepareOptionsMenu 未被调用,MenuItem 未显示
onPrepareOptionsMenu not being called, MenuItem not showing
我有三个 fragments
:R.id.splashFragment
、R.id.selectionFragment
和 R.id.userSettingsFragment
我有一个 MenuItem
叫做 settings
我希望应用如何工作:
splashFragment
为主画面。
然后当我点击splashFragment
中的Login Button
时,屏幕显示selectionFragment
和MenuItem
。
最后,当我点击 MenuItem
时,屏幕被重定向到 userSettingsFragment
。
问题:
当我点击 Login Button
时,屏幕成功显示 selectionFragment
。但是,MenuItem
没有显示,因此我也看不到 userSettingsFragment
。
为了显示 MenuItem
,我覆盖了 onPrepareOptionsMenu
方法。然而,在调试之后,我注意到 onPrepareOptionsMenu
永远不会被调用,因此 MenuItem
永远不会被显示。
谁能解释一下为什么 onPrepareOptionsMenu
从来没有被调用过?或者为什么 MenuItem
没有显示在屏幕上并可能提供解决方案?
谢谢。
EDITED CODE 包含更多片段代码
已编辑代码 现在包括所有代码
代码:
public class MainActivity extends FragmentActivity {
private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int SETTINGS = 2;
private static final int FRAGMENT_COUNT = SETTINGS +1;
private MenuItem settings;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private boolean isResumed = false;
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback =
new Session.StatusCallback() {
@Override
public void call(Session session,
SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// only add the menu when the selection fragment is showing
if (fragments[SELECTION].isVisible()) {
if (menu.size() == 0) {
settings = menu.add(R.string.settings);
}
return true;
} else {
menu.clear();
settings = null;
}
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.equals(settings)) {
showFragment(SETTINGS, true);
return true;
}
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm = getSupportFragmentManager();
fragments[SPLASH] = fm.findFragmentById(R.id.splashFragment);
fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);
fragments[SETTINGS] = fm.findFragmentById(R.id.userSettingsFragment);
FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
}
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
@Override
public void onResume() {
super.onResume();
uiHelper.onResume();
isResumed = true;
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
isResumed = false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Only make changes if the activity is visible
if (isResumed) {
FragmentManager manager = getSupportFragmentManager();
// Get the number of entries in the back stack
int backStackSize = manager.getBackStackEntryCount();
// Clear the back stack
for (int i = 0; i < backStackSize; i++) {
manager.popBackStack();
}
if (state.isOpened()) {
// If the session state is open:
// Show the authenticated fragment
// invalidateOptionsMenu(); ???
showFragment(SELECTION, false);
} else if (state.isClosed()) {
// If the session state is closed:
// Show the login fragment
showFragment(SPLASH, false);
}
}
}
@Override
protected void onResumeFragments() {
super.onResumeFragments();
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// if the session is already open,
// try to show the selection fragment
// invalidateOptionsMenu(); ???
showFragment(SELECTION, false);
} else {
// otherwise present the splash screen
// and ask the person to login.
showFragment(SPLASH, false);
}
}
}
我对此很陌生。 onPrepareOptionsMenu() 可能没问题——我还没有查过。但是,在我的 FragmentActivity 中,我使用的是 onCreateOptionsMenu():
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu
getMenuInflater().inflate(R.menu.menu_page_view, menu);
return true;
}
切换到 SelectionFragment
时,您必须调用 invalidateOptionsMenu()
。这会强制系统刷新操作项,从而召回 onPrepareOptionsMenu()
.
从Fragment代码中,你得先得到你activity:getActivity().invalidateOptionsMenu();
我有三个 fragments
:R.id.splashFragment
、R.id.selectionFragment
和 R.id.userSettingsFragment
我有一个 MenuItem
叫做 settings
我希望应用如何工作:
splashFragment
为主画面。
然后当我点击splashFragment
中的Login Button
时,屏幕显示selectionFragment
和MenuItem
。
最后,当我点击 MenuItem
时,屏幕被重定向到 userSettingsFragment
。
问题:
当我点击 Login Button
时,屏幕成功显示 selectionFragment
。但是,MenuItem
没有显示,因此我也看不到 userSettingsFragment
。
为了显示 MenuItem
,我覆盖了 onPrepareOptionsMenu
方法。然而,在调试之后,我注意到 onPrepareOptionsMenu
永远不会被调用,因此 MenuItem
永远不会被显示。
谁能解释一下为什么 onPrepareOptionsMenu
从来没有被调用过?或者为什么 MenuItem
没有显示在屏幕上并可能提供解决方案?
谢谢。
EDITED CODE 包含更多片段代码 已编辑代码 现在包括所有代码
代码:
public class MainActivity extends FragmentActivity {
private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int SETTINGS = 2;
private static final int FRAGMENT_COUNT = SETTINGS +1;
private MenuItem settings;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private boolean isResumed = false;
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback =
new Session.StatusCallback() {
@Override
public void call(Session session,
SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// only add the menu when the selection fragment is showing
if (fragments[SELECTION].isVisible()) {
if (menu.size() == 0) {
settings = menu.add(R.string.settings);
}
return true;
} else {
menu.clear();
settings = null;
}
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.equals(settings)) {
showFragment(SETTINGS, true);
return true;
}
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm = getSupportFragmentManager();
fragments[SPLASH] = fm.findFragmentById(R.id.splashFragment);
fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);
fragments[SETTINGS] = fm.findFragmentById(R.id.userSettingsFragment);
FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
}
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
@Override
public void onResume() {
super.onResume();
uiHelper.onResume();
isResumed = true;
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
isResumed = false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Only make changes if the activity is visible
if (isResumed) {
FragmentManager manager = getSupportFragmentManager();
// Get the number of entries in the back stack
int backStackSize = manager.getBackStackEntryCount();
// Clear the back stack
for (int i = 0; i < backStackSize; i++) {
manager.popBackStack();
}
if (state.isOpened()) {
// If the session state is open:
// Show the authenticated fragment
// invalidateOptionsMenu(); ???
showFragment(SELECTION, false);
} else if (state.isClosed()) {
// If the session state is closed:
// Show the login fragment
showFragment(SPLASH, false);
}
}
}
@Override
protected void onResumeFragments() {
super.onResumeFragments();
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// if the session is already open,
// try to show the selection fragment
// invalidateOptionsMenu(); ???
showFragment(SELECTION, false);
} else {
// otherwise present the splash screen
// and ask the person to login.
showFragment(SPLASH, false);
}
}
}
我对此很陌生。 onPrepareOptionsMenu() 可能没问题——我还没有查过。但是,在我的 FragmentActivity 中,我使用的是 onCreateOptionsMenu():
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu
getMenuInflater().inflate(R.menu.menu_page_view, menu);
return true;
}
切换到 SelectionFragment
时,您必须调用 invalidateOptionsMenu()
。这会强制系统刷新操作项,从而召回 onPrepareOptionsMenu()
.
从Fragment代码中,你得先得到你activity:getActivity().invalidateOptionsMenu();