片段onKeyDown()无法正常工作

时间:2015-08-17 06:07:08

标签: android android-fragments

我有以FragOne调用FragTest和FragTest调用FragBook的顺序调用的片段。当我的当前片段是按下后退按钮时的FragBook时,FragTest只是暂时显示,然后我的应用程序被发送到应用程序托盘。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    FragmentManager fragManager = getSupportFragmentManager();
    Fragment frag = fragManager.findFragmentByTag("Frag_Book");

    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        if (frag.isVisible()) {
            fragManager.beginTransaction()
                    .replace(R.id.container, Fragment.instantiate(MainActivity.this, fragments[1]),"Frag_Test")
                    .commit();
        }
    }
    return super.onKeyDown(keyCode, event);
}

我在哪里出错?感谢...

1 个答案:

答案 0 :(得分:0)

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        FragmentManager fragManager = getSupportFragmentManager();
        Fragment frag = fragManager.findFragmentByTag("Frag_Book");

        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if (frag.isVisible()) {
                fragManager.beginTransaction()
                        .replace(R.id.container, Fragment.instantiate(MainActivity.this, fragments[1]),"Frag_Test")
                        .commit();
             return true; //"------------- should be add"
            }
        }
        return super.onKeyDown(keyCode, event);
    }