Don't move to HotspotFragment on rotate when user navigated back to introduction

This commit is contained in:
Sebastian Kürten
2021-06-28 16:13:16 +02:00
parent b15d42b0cd
commit 9370062e41
3 changed files with 27 additions and 4 deletions

View File

@@ -54,11 +54,14 @@ public class HotspotActivity extends BriarActivity
FragmentManager fm = getSupportFragmentManager();
viewModel.getState().observe(this, hotspotState -> {
if (hotspotState instanceof HotspotStarted) {
HotspotStarted started = (HotspotStarted) hotspotState;
String tag = HotspotFragment.TAG;
// check if fragment is already added
// to not lose state on configuration changes
if (fm.findFragmentByTag(tag) == null) {
showFragment(fm, new HotspotFragment(), tag);
if (!started.consume()) {
showFragment(fm, new HotspotFragment(), tag);
}
}
} else if (hotspotState instanceof HotspotError) {
HotspotError error = ((HotspotError) hotspotState);

View File

@@ -5,6 +5,7 @@ import android.graphics.Bitmap;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
@NotNullByDefault
abstract class HotspotState {
@@ -38,6 +39,9 @@ abstract class HotspotState {
static class HotspotStarted extends HotspotState {
private final NetworkConfig networkConfig;
private final WebsiteConfig websiteConfig;
// 'consumed' is set to true once this state triggered a UI change, i.e.
// moving to the next fragment.
private boolean consumed = false;
HotspotStarted(NetworkConfig networkConfig,
WebsiteConfig websiteConfig) {
@@ -52,6 +56,18 @@ abstract class HotspotState {
WebsiteConfig getWebsiteConfig() {
return websiteConfig;
}
/**
* Mark this state as consumed, i.e. the UI has already done something
* as a result of the state changing to this. This can be used in order
* to not repeat actions such as showing fragments on rotation changes.
*/
@UiThread
boolean consume() {
boolean old = consumed;
consumed = true;
return old;
}
}
static class HotspotError extends HotspotState {

View File

@@ -96,9 +96,13 @@ class HotspotViewModel extends DbViewModel
void startHotspot() {
HotspotState s = state.getValue();
if (s instanceof HotspotStarted) {
// Don't try to start again, if already started, just re-set value.
// This can happen if the user navigates back to intro fragment.
state.setValue(s);
// This can happen if the user navigates back to intro fragment and
// taps 'start sharing' again. In this case, don't try to start the
// hotspot again. Instead, just create a new, unconsumed HotspotStarted
// event with the same config.
HotspotStarted old = (HotspotStarted) s;
state.setValue(new HotspotStarted(old.getNetworkConfig(),
old.getWebsiteConfig()));
} else {
hotspotManager.startWifiP2pHotspot();
notificationManager.showHotspotNotification();