Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions app/src/main/java/fr/free/nrw/commons/explore/ExploreFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class ExploreFragment : CommonsDaggerSupportFragment() {
override fun onPageScrollStateChanged(state: Int) = Unit
override fun onPageSelected(position: Int) {
binding!!.viewPager.canScroll = position != 2
// based on which tab we are now recreate the options menu
activity?.invalidateOptionsMenu()

if (position == 2) {
mapRootFragment?.requestLocationIfNeeded()
}
Expand Down Expand Up @@ -178,19 +181,6 @@ class ExploreFragment : CommonsDaggerSupportFragment() {
if (binding!!.viewPager.currentItem == 2) {
others.setVisible(true)
}

// if on Map tab, show all menu options, else only show search
binding!!.viewPager.addOnPageChangeListener(object : OnPageChangeListener {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) = Unit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these functions no longer needed? Is this change really required to fix the issue?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions are still needed, but only one instance of the OnPageChangeListener is required.
Before this change, the same ViewPager.OnPageChangeListener logic was being registered in two different lifecycle methods:
once in onCreateView()
and again in onCreateOptionsMenu()
Since onCreateOptionsMenu() can be invoked multiple times during the fragment lifecycle, this resulted in multiple listeners being added over time.
Each listener triggers:
mapRootFragment?.requestLocationIfNeeded()
when the Map tab is selected, so switching to the Map tab caused requestLocationIfNeeded() to be called multiple times, which led to the duplicate location-off dialogs.
The listener in onCreateView() already handles:
detecting when the Map tab is selected
triggering requestLocationIfNeeded() exactly once per tab selection
Therefore, the additional listener in onCreateOptionsMenu() was redundant and was the source of the repeated calls. Removing it ensures the logic runs only once, while preserving all existing behavior.

override fun onPageScrollStateChanged(state: Int) = Unit
override fun onPageSelected(position: Int) {
binding!!.viewPager.canScroll = position != 2
others.setVisible(position == 2)
if (position == 2) {
mapRootFragment?.requestLocationIfNeeded()
}
}
})
} else {
inflater.inflate(R.menu.menu_search, menu)
}
Expand Down