From 6044dab0c9d243a791b9e60bba28a93b06b1c2b5 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 4 Aug 2018 21:26:55 +0200 Subject: [PATCH] lets just restart the activity at Shown Tabs change to avoid glitches --- .../contacts/activities/MainActivity.kt | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt index f1386655..f0f6798f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt @@ -8,6 +8,7 @@ import android.content.pm.PackageManager import android.graphics.drawable.ColorDrawable import android.net.Uri import android.os.Bundle +import android.os.Handler import android.support.v4.app.ActivityCompat import android.support.v4.content.ContextCompat import android.support.v4.view.MenuItemCompat @@ -102,7 +103,9 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } if (storedShowTabs != config.showTabs) { - viewpager.adapter = null + config.lastUsedViewPagerPage = 0 + System.exit(0) + return } val configShowContactThumbnails = config.showContactThumbnails @@ -220,7 +223,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { override fun onQueryTextChange(newText: String): Boolean { if (isSearchOpen) { - getCurrentFragment().onSearchQueryChanged(newText) + getCurrentFragment()?.onSearchQueryChanged(newText) } return true } @@ -229,20 +232,20 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { MenuItemCompat.setOnActionExpandListener(searchMenuItem, object : MenuItemCompat.OnActionExpandListener { override fun onMenuItemActionExpand(item: MenuItem?): Boolean { - getCurrentFragment().onSearchOpened() + getCurrentFragment()?.onSearchOpened() isSearchOpen = true return true } override fun onMenuItemActionCollapse(item: MenuItem?): Boolean { - getCurrentFragment().onSearchClosed() + getCurrentFragment()?.onSearchClosed() isSearchOpen = false return true } }) } - private fun getCurrentFragment(): MyViewPagerFragment { + private fun getCurrentFragment(): MyViewPagerFragment? { val showTabs = config.showTabs val fragments = arrayListOf() if (showTabs and CONTACTS_TAB_MASK != 0) { @@ -297,14 +300,14 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } } - private fun getInactiveTabIndexes(activeIndex: Int) = arrayListOf(0, 1, 2).filter { it != activeIndex } + private fun getInactiveTabIndexes(activeIndex: Int) = arrayListOf(0, 1, 2, 3).filter { it != activeIndex } private fun initFragments() { viewpager.offscreenPageLimit = 3 viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener { override fun onPageScrollStateChanged(state: Int) { if (isSearchOpen) { - getCurrentFragment().onSearchQueryChanged("") + getCurrentFragment()?.onSearchQueryChanged("") searchMenuItem?.collapseActionView() } } @@ -331,7 +334,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { }, tabSelectedAction = { if (isSearchOpen) { - getCurrentFragment().onSearchQueryChanged("") + getCurrentFragment()?.onSearchQueryChanged("") searchMenuItem?.collapseActionView() } viewpager.currentItem = it.position @@ -354,6 +357,13 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } } + // selecting the proper tab sometimes glitches, add an extra selector to make sure we have it right + main_tabs_holder.onGlobalLayout { + Handler().postDelayed({ + main_tabs_holder.getTabAt(config.lastUsedViewPagerPage)?.select() + }, 100L) + } + main_tabs_holder.beVisibleIf(skippedTabs < 3) invalidateOptionsMenu() }