0
votes

I have an acivity with appbar and a fullsecreen dialogfragment with appbar (that dialog is called from the activity). I have set some action when the home button of the activity is pressed, and when the home button og the dialogfragment is pressed it shoud close the dialog. I have notice that the two buttons have the same id (android.R.id.home). and aparently there is a conflict when the method "onOptionsItemSelected" is called because when I press the home button of the dialog it doesn't work, but if a remove the portion of the code on the activity (if id == android.R.id.home) it works fine and the dialog dismiss. What should I do?. is there way to prevent this conflict , maybe set a diferent id for the home button?

this is the method of the activity

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);

    }

this is the method of the dialogfragment

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

1 Answers

0
votes

You can apply and try this solution in activity.

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);

}