1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 14:21:16 +00:00

Fix crash in MainActivity.onSaveInstanceState()

This commit is contained in:
Felix Ableitner 2017-09-27 12:35:34 +09:00
parent bdec750b34
commit 8e1dfe875e

View file

@ -42,6 +42,7 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.annimon.stream.function.Consumer;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.fragments.DeviceListFragment; import com.nutomic.syncthingandroid.fragments.DeviceListFragment;
import com.nutomic.syncthingandroid.fragments.DrawerFragment; import com.nutomic.syncthingandroid.fragments.DrawerFragment;
@ -272,12 +273,17 @@ public class MainActivity extends StateDialogActivity
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
// Avoid crash if called during startup.
if (mFolderListFragment != null && mDeviceListFragment != null && mDrawerFragment != null) {
FragmentManager fm = getSupportFragmentManager(); FragmentManager fm = getSupportFragmentManager();
fm.putFragment(outState, FolderListFragment.class.getName(), mFolderListFragment); Consumer<Fragment> putFragment = fragment -> {
fm.putFragment(outState, DeviceListFragment.class.getName(), mDeviceListFragment); if (fragment != null && fragment.isAdded()) {
fm.putFragment(outState, DrawerFragment.class.getName(), mDrawerFragment); fm.putFragment(outState, fragment.getClass().getName(), fragment);
}
};
putFragment.accept(mFolderListFragment);
putFragment.accept(mDeviceListFragment);
putFragment.accept(mDrawerFragment);
outState.putInt("currentTab", mViewPager.getCurrentItem()); outState.putInt("currentTab", mViewPager.getCurrentItem());
outState.putBoolean(BATTERY_DIALOG_DISMISSED, mBatteryOptimizationsDialog == null || !mBatteryOptimizationsDialog.isShowing()); outState.putBoolean(BATTERY_DIALOG_DISMISSED, mBatteryOptimizationsDialog == null || !mBatteryOptimizationsDialog.isShowing());
outState.putBoolean(IS_SHOWING_RESTART_DIALOG, mRestartDialog != null && mRestartDialog.isShowing()); outState.putBoolean(IS_SHOWING_RESTART_DIALOG, mRestartDialog != null && mRestartDialog.isShowing());
@ -290,7 +296,6 @@ public class MainActivity extends StateDialogActivity
} }
Util.dismissDialogSafe(mRestartDialog, this); Util.dismissDialogSafe(mRestartDialog, this);
} }
}
@Override @Override
protected void onPostCreate(Bundle savedInstanceState) { protected void onPostCreate(Bundle savedInstanceState) {