活动主页按钮和Dialogfragment主页按钮ID

时间:2017-04-22 04:43:37

标签: android android-dialogfragment android-identifiers

我对appbar有一个动作,并且有一个带有appbar的fullsecreen dialogfragment(该对话框是从活动中调用的)。 我按下活动的主页按钮时设置了一些动作,当按下主页按钮og按下对话框时,它会关闭对话框。 我注意到这两个按钮具有相同的id(android.R.id.home)。当方法" onOptionsItemSelected"并且显然存在冲突。被调用是因为当我按下对话框的主页按钮时它不起作用,但是如果删除了活动上的代码部分(如果id == android.R.id.home) 它工作正常,对话消失。 我该怎么办?。有没有办法防止这种冲突,可能为主页按钮设置不同的ID?

这是活动的方法

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_logout) {
            exitApp();
            return true;
        }

        else if ((id == android.R.id.home) && searchActivated) {
            drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
           toggle.syncState();
            searchActivated=false;
            reload_fragment_data();
            return true;
        }
        else if ((id == android.R.id.home) && (!searchActivated))
        {
            drawer.openDrawer(GravityCompat.START);  // OPEN DRAWER
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

这是对话片段的方法

public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_next) {
            pager.setCurrentItem(pager.getCurrentItem() + 1);
           updateTitle();
            return true;
        } else if (id==R.id.action_previous)
        {

            pager.setCurrentItem(pager.getCurrentItem() - 1);
           updateTitle();
            return true;
        }

        else if (id == android.R.id.home) {

            dismiss();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

1 个答案:

答案 0 :(得分:0)

您可以在活动中申请并尝试此解决方案。

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_container);
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_logout) {
        exitApp();
        return true;
    }

    else if ((id == android.R.id.home) && searchActivated && !(fragment instanceof DialogFragmentClassName)) {
        drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        toggle.syncState();
        searchActivated=false;
        reload_fragment_data();
        return true;
    }
    else if ((id == android.R.id.home) && (!searchActivated))
    {
        drawer.openDrawer(GravityCompat.START);  // OPEN DRAWER
        return true;
    }
    return super.onOptionsItemSelected(item);

}
相关问题