diff options
author | cyBerta <cyberta@riseup.net> | 2019-04-02 01:12:37 +0200 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2019-04-02 01:12:37 +0200 |
commit | 197f79fa142c2c6bfe2d5e7279c6fad79fa427c2 (patch) | |
tree | ffc94f0256206746346fc14da2438c021a0b9f1a | |
parent | 9eb20164f227fb617b2ea9700ebd493f9bf7caa4 (diff) |
catch illegal state exceptions when replacing fragments
6 files changed, 23 insertions, 24 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/FragmentManagerEnhanced.java b/app/src/main/java/se/leap/bitmaskclient/FragmentManagerEnhanced.java index 9fe4fd23..a612820d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/FragmentManagerEnhanced.java +++ b/app/src/main/java/se/leap/bitmaskclient/FragmentManagerEnhanced.java @@ -40,9 +40,17 @@ public class FragmentManagerEnhanced { } public void replace(int containerViewId, Fragment fragment, String tag) { - FragmentTransaction transaction = genericFragmentManager.beginTransaction(); + try { + if (genericFragmentManager.findFragmentByTag(tag) != null) { + FragmentTransaction transaction = genericFragmentManager.beginTransaction(); + transaction.replace(containerViewId, fragment, tag).commit(); + } else { + genericFragmentManager.beginTransaction().add(containerViewId, fragment, tag).commit(); + } + } catch (IllegalStateException e) { + e.printStackTrace(); + } - transaction.replace(containerViewId, fragment, tag).commit(); } public FragmentTransaction beginTransaction() { diff --git a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java index fd0c27d6..b0acd860 100644 --- a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java @@ -101,14 +101,13 @@ public class MainActivity extends AppCompatActivity implements EipSetupListener, @Override public void onBackPressed() { FragmentManagerEnhanced fragmentManagerEnhanced = new FragmentManagerEnhanced(getSupportFragmentManager()); - if (fragmentManagerEnhanced.findFragmentByTag(EipFragment.TAG) == null) { - Fragment fragment = new EipFragment(); + Fragment fragment = fragmentManagerEnhanced.findFragmentByTag(MainActivity.TAG); + if (fragment == null || !(fragment instanceof EipFragment)) { + Fragment eipFragment = new EipFragment(); Bundle bundle = new Bundle(); bundle.putParcelable(PROVIDER_KEY, provider); - fragment.setArguments(bundle); - fragmentManagerEnhanced.beginTransaction() - .replace(R.id.container, fragment, EipFragment.TAG) - .commit(); + eipFragment.setArguments(bundle); + fragmentManagerEnhanced.replace(R.id.main_container, eipFragment, MainActivity.TAG); } else { super.onBackPressed(); } @@ -126,12 +125,10 @@ public class MainActivity extends AppCompatActivity implements EipSetupListener, return; } - String fragmentTag = null; Fragment fragment = null; switch (intent.getAction()) { case ACTION_SHOW_VPN_FRAGMENT: fragment = new EipFragment(); - fragmentTag = EipFragment.TAG; Bundle bundle = new Bundle(); if (intent.hasExtra(ASK_TO_CANCEL_VPN)) { bundle.putBoolean(ASK_TO_CANCEL_VPN, true); @@ -150,9 +147,8 @@ public class MainActivity extends AppCompatActivity implements EipSetupListener, intent.setAction(null); if (fragment != null) { - new FragmentManagerEnhanced(getSupportFragmentManager()).beginTransaction() - .replace(R.id.container, fragment, fragmentTag) - .commit(); + new FragmentManagerEnhanced(getSupportFragmentManager()) + .replace(R.id.main_container, fragment, MainActivity.TAG); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java b/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java index a01a79ea..21d061d1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java @@ -29,7 +29,6 @@ import android.os.Handler; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; @@ -38,7 +37,6 @@ import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -53,6 +51,7 @@ import se.leap.bitmaskclient.DrawerSettingsAdapter; import se.leap.bitmaskclient.DrawerSettingsAdapter.DrawerSettingsItem; import se.leap.bitmaskclient.EipFragment; import se.leap.bitmaskclient.FragmentManagerEnhanced; +import se.leap.bitmaskclient.MainActivity; import se.leap.bitmaskclient.Provider; import se.leap.bitmaskclient.ProviderListActivity; import se.leap.bitmaskclient.R; @@ -461,20 +460,17 @@ public class NavigationDrawerFragment extends Fragment { public void onTextItemSelected(AdapterView<?> parent, int position) { // update the main content by replacing fragments - FragmentManager fragmentManager = getFragmentManager(); + FragmentManagerEnhanced fragmentManager = new FragmentManagerEnhanced(getActivity().getSupportFragmentManager()); Fragment fragment = null; - String fragmentTag = null; if (parent == drawerAccountsListView) { title = getString(R.string.vpn_fragment_title); fragment = new EipFragment(); - fragmentTag = EipFragment.TAG; Bundle arguments = new Bundle(); Provider currentProvider = getSavedProviderFromSharedPreferences(preferences); arguments.putParcelable(PROVIDER_KEY, currentProvider); fragment.setArguments(arguments); } else { - Log.d(TAG, String.format("Selected position %d", position)); DrawerSettingsItem settingsItem = settingsListAdapter.getItem(position); switch (settingsItem.getItemType()) { case SWITCH_PROVIDER: @@ -507,9 +503,7 @@ public class NavigationDrawerFragment extends Fragment { } if (fragment != null) { - fragmentManager.beginTransaction() - .replace(R.id.container, fragment, fragmentTag) - .commit(); + fragmentManager.replace(R.id.main_container, fragment, MainActivity.TAG); } restoreActionBar(); diff --git a/app/src/main/java/se/leap/bitmaskclient/fragments/AboutFragment.java b/app/src/main/java/se/leap/bitmaskclient/fragments/AboutFragment.java index 25249371..1a2e2df0 100644 --- a/app/src/main/java/se/leap/bitmaskclient/fragments/AboutFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/fragments/AboutFragment.java @@ -18,7 +18,7 @@ import static android.view.View.VISIBLE; public class AboutFragment extends Fragment { - final public static String TAG = "aboutFragment"; + final public static String TAG = AboutFragment.class.getSimpleName(); final public static int VIEWED = 0; @InjectView(R.id.version) diff --git a/app/src/main/java/se/leap/bitmaskclient/fragments/LogFragment.java b/app/src/main/java/se/leap/bitmaskclient/fragments/LogFragment.java index 03e0669a..268d326d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/fragments/LogFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/fragments/LogFragment.java @@ -68,6 +68,7 @@ import se.leap.bitmaskclient.R; import static de.blinkt.openvpn.core.OpenVPNService.humanReadableByteCount; public class LogFragment extends ListFragment implements StateListener, SeekBar.OnSeekBarChangeListener, RadioGroup.OnCheckedChangeListener, VpnStatus.ByteCountListener { + public static final String TAG = LogFragment.class.getSimpleName(); private static final String LOGTIMEFORMAT = "logtimeformat"; private static final int START_VPN_CONFIG = 0; private static final String VERBOSITYLEVEL = "verbositylevel"; diff --git a/app/src/main/res/layout/a_main.xml b/app/src/main/res/layout/a_main.xml index 21fdaa66..0344e5af 100644 --- a/app/src/main/res/layout/a_main.xml +++ b/app/src/main/res/layout/a_main.xml @@ -26,7 +26,7 @@ </android.support.v7.widget.Toolbar> <FrameLayout - android:id="@+id/container" + android:id="@+id/main_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> |