Search code examples
androidandroid-fragmentsandroid-viewpagerfragment

How can I know when focus is on one of the fragments inside the viewpager?


I created five fragments inside the view pager.

All fragments are created as the activity containing the viewpager is executed.

Therefore, the method of the fragment is executed as soon as the activity is executed.

In other parts this is what I intended.

However, this method raises another activity via Intent, which I did not want.

The point at which I want this method to execute is when the fragment is put on the screen.

Is there a method that works when a fragment inside the view pager that is already created is displayed on the screen?

The general life cycle method of a fragment can not be used because it works concurrently with activity execution.

If you know of any way I can use it, I would be grateful to let you know.

Have a good day :)


Solution

  • Override this method in fragment

    @Override
        public void setMenuVisibility(boolean menuVisible) {
            super.setMenuVisibility(menuVisible);
            //if fragment is visible then menuVisible = true else false
        }