0

I displayed a fragment A that implements a ViewPager with several fragments (nested fragments).

In my nested fragments, I inflate a menu with the following method.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.my_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

This question was already asked here. And i tried all the answers its not working.

My issue is Everything working fine.but when i open another fragment(it have not any option menu) and get back to previous view pager fragment while clicking menu item onOptionsItemSelected not firing. When i swipe fragment in viewpager and come back to the previous one, when i click menu item its firing.

1 Answer 1

1

Its because viewpager maintain 3 fragment alive at a time. so when you come back, it set menu visibility status true to last fragment. thats why your menu item click not firing.

Use the following in the fragment where you keeping a viewpager in your case fragment A.

private boolean isInitial=true;

@Override
    public void onResume() {
        super.onResume();

        if (!isInitial) {
            int pos = viewpager.getCurrentItem();
            if (pageAdapter.getItem(pos).getUserVisibleHint() && pageAdapter.getItem(pos).isVisible()) {
                pageAdapter.getItem(pos).setMenuVisibility(true);
            }
        } else {
            isInitial = false;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.