Commit 197f79fa authored by cyberta's avatar cyberta

catch illegal state exceptions when replacing fragments

parent 9eb20164
......@@ -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() {
......
......@@ -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);
}
}
......
......@@ -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();
......
......@@ -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)
......
......@@ -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";
......
......@@ -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>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment