首页 > 解决方案 > 删除 OnBackPressedCallback 片段管理器

问题描述

我有一个从互联网加载数据的活动,它工作正常,除了必须双击后退按钮来完成它。我还注意到,在未连接到互联网的活动中,后退按钮从第一次单击开始起作用。调试时我发现片段管理器回调正在处理第一次点击

        FragmentManager.this.handleOnBackPressed();

从源头

void handleOnBackPressed() {
    // First, execute any pending actions to make sure we're in an
    // up to date view of the world just in case anyone is queuing
    // up transactions that change the back stack then immediately
    // calling onBackPressed()
    execPendingActions();
    if (mOnBackPressedCallback.isEnabled()) {
        // We still have a back stack, so we can pop
        popBackStackImmediate();
    } else {
        // Sigh. Due to FragmentManager's asynchronicity, we can
        // get into cases where we *think* we can handle the back
        // button but because of frame perfect dispatch, we fell
        // on our face. Since our callback is disabled, we can
        // re-trigger the onBackPressed() to dispatch to the next
        // enabled callback
        mOnBackPressedDispatcher.onBackPressed();
    }
}

虽然我不使用片段

标签: androidfragmentmanageronbackpressed

解决方案


推荐阅读