From 39e1329f41f0d4ae7f9deff0f6ea1cce44ec1f71 Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Fri, 26 Oct 2018 17:23:25 +0200 Subject: [PATCH] Follow-Up to issue 108 "IllegalStateEx" workaround (fixes #108) (#113) * MainActivity updateViewPager - Add isServiceActive * Add numPages, setOffscreenPageLimit (fixes #108) * Fix numPages --- .../syncthingandroid/activities/MainActivity.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java b/app/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java index a1701d9e..ffc05634 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java @@ -211,12 +211,14 @@ public class MainActivity extends SyncthingActivity * Updates the ViewPager to show tabs depending on the service state. */ private void updateViewPager() { + Boolean isServiceActive = mSyncthingServiceState == SyncthingService.State.ACTIVE; + final int numPages = (isServiceActive ? 3 : 1); FragmentStatePagerAdapter mSectionsPagerAdapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { - if (mSyncthingServiceState == SyncthingService.State.ACTIVE) { + if (isServiceActive) { switch (position) { case 0: return mFolderListFragment; @@ -244,12 +246,12 @@ public class MainActivity extends SyncthingActivity @Override public int getCount() { - return mSyncthingServiceState == SyncthingService.State.ACTIVE ? 3 : 1; + return numPages; } @Override public CharSequence getPageTitle(int position) { - if (mSyncthingServiceState == SyncthingService.State.ACTIVE) { + if (isServiceActive) { switch (position) { case 0: return getResources().getString(R.string.folders_fragment_title); @@ -286,6 +288,7 @@ public class MainActivity extends SyncthingActivity .setPositiveButton(android.R.string.ok, (dialog, which) -> {}) .show(); } + mViewPager.setOffscreenPageLimit(numPages); TabLayout tabLayout = findViewById(R.id.tabContainer); tabLayout.setupWithViewPager(mViewPager); }