小编dar*_*son的帖子

拦截Fragment中的ActionBar Home按钮

我可以成功拦截ActionBar来自 my的home 按钮NavigationDrawerFragment,它被添加到 my 中MainActivity,如下所示:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (!loggedIn() && item.getItemId() == android.R.id.home) {
        login();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)

然而,在我ComposeActivityComposeFragment这不起作用。onOptionsItemSelected不在片段上调用。

我已经调试了代码,问题似乎归结为 Android 支持库的设计。似乎FragmentActivityActivity都有自己对FragmentManager.

FragmentActivityActivity在检查其任何片段之前首先检查是否可以处理该事件,这与文档一致:

/**
 * Dispatch context and options menu to fragments.
 */
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    if (super.onMenuItemSelected(featureId, item)) {
        return true;
    }

    switch (featureId) { …
Run Code Online (Sandbox Code Playgroud)

android android-appcompat android-fragments android-actionbar android-fragmentactivity

5
推荐指数
1
解决办法
6248
查看次数