Fix ActivityResultLauncher lifecycle issues
If an activity is re-created, its result launchers that were registered during instantiation are not available, so register those in onCreate instead and unregister in onDestroy. Change-Id: I7fd2a61ddbb056e0bf761b6918dc371db8ddbe33
This commit is contained in:
committed by
Michael Bestas
parent
4ed687c330
commit
0d3264d67f
@@ -50,10 +50,7 @@ public abstract class BaseSetupWizardActivity extends AppCompatActivity implemen
|
||||
|
||||
private NavigationLayout mNavigationBar;
|
||||
|
||||
private final ActivityResultLauncher<Intent> mNextIntentResultLauncher =
|
||||
registerForActivityResult(
|
||||
new StartDecoratedActivityForResult(),
|
||||
BaseSetupWizardActivity.this::onNextIntentResult);
|
||||
private ActivityResultLauncher<Intent> mNextIntentResultLauncher;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
@@ -61,6 +58,9 @@ public abstract class BaseSetupWizardActivity extends AppCompatActivity implemen
|
||||
logActivityState("onCreate savedInstanceState=" + savedInstanceState);
|
||||
}
|
||||
super.onCreate(savedInstanceState);
|
||||
mNextIntentResultLauncher = registerForActivityResult(
|
||||
new StartDecoratedActivityForResult(),
|
||||
BaseSetupWizardActivity.this::onNextIntentResult);
|
||||
initLayout();
|
||||
mNavigationBar = getNavigationBar();
|
||||
if (mNavigationBar != null) {
|
||||
@@ -125,6 +125,7 @@ public abstract class BaseSetupWizardActivity extends AppCompatActivity implemen
|
||||
logActivityState("onDestroy");
|
||||
}
|
||||
super.onDestroy();
|
||||
mNextIntentResultLauncher.unregister();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -28,10 +28,7 @@ public abstract class SubBaseActivity extends BaseSetupWizardActivity {
|
||||
|
||||
protected abstract void onStartSubactivity();
|
||||
|
||||
private final ActivityResultLauncher<Intent> mSubactivityResultLauncher =
|
||||
registerForActivityResult(
|
||||
new StartDecoratedActivityForResult(),
|
||||
SubBaseActivity.this::onSubactivityResult);
|
||||
private ActivityResultLauncher<Intent> mSubactivityResultLauncher;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -39,12 +36,21 @@ public abstract class SubBaseActivity extends BaseSetupWizardActivity {
|
||||
Log.d(TAG, "onCreate savedInstanceState=" + savedInstanceState);
|
||||
}
|
||||
super.onCreate(savedInstanceState);
|
||||
mSubactivityResultLauncher = registerForActivityResult(
|
||||
new StartDecoratedActivityForResult(),
|
||||
SubBaseActivity.this::onSubactivityResult);
|
||||
setNextAllowed(false);
|
||||
if (savedInstanceState == null) {
|
||||
onStartSubactivity();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
mSubactivityResultLauncher.unregister();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
Reference in New Issue
Block a user