diff --git a/packages/SystemUI/Android.bp b/packages/SystemUI/Android.bp index ceeab354cee99..a544d9b7af399 100644 --- a/packages/SystemUI/Android.bp +++ b/packages/SystemUI/Android.bp @@ -223,6 +223,7 @@ android_library { "androidx.test.rules", "androidx.test.uiautomator_uiautomator", "mockito-target-extended-minus-junit4", + "androidx.test.ext.junit", "testables", "truth-prebuilt", "monet", diff --git a/packages/SystemUI/src/com/android/systemui/user/CreateUserActivity.java b/packages/SystemUI/src/com/android/systemui/user/CreateUserActivity.java index f01712653e1b0..b56c4034936ff 100644 --- a/packages/SystemUI/src/com/android/systemui/user/CreateUserActivity.java +++ b/packages/SystemUI/src/com/android/systemui/user/CreateUserActivity.java @@ -25,6 +25,8 @@ import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.RemoteException; import android.util.Log; +import android.window.OnBackInvokedCallback; +import android.window.OnBackInvokedDispatcher; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -59,6 +61,7 @@ public class CreateUserActivity extends Activity { private final ActivityStarter mActivityStarter; private Dialog mSetupUserDialog; + private final OnBackInvokedCallback mBackCallback = this::onBackInvoked; @Inject public CreateUserActivity(UserCreator userCreator, @@ -82,6 +85,10 @@ public class CreateUserActivity extends Activity { mSetupUserDialog = createDialog(); mSetupUserDialog.show(); + + getOnBackInvokedDispatcher().registerOnBackInvokedCallback( + OnBackInvokedDispatcher.PRIORITY_DEFAULT, + mBackCallback); } @Override @@ -125,10 +132,20 @@ public class CreateUserActivity extends Activity { @Override public void onBackPressed() { - super.onBackPressed(); + onBackInvoked(); + } + + private void onBackInvoked() { if (mSetupUserDialog != null) { mSetupUserDialog.dismiss(); } + finish(); + } + + @Override + protected void onDestroy() { + getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(mBackCallback); + super.onDestroy(); } private void addUserNow(String userName, Drawable userIcon) { diff --git a/packages/SystemUI/tests/AndroidManifest.xml b/packages/SystemUI/tests/AndroidManifest.xml index 1b404a82145b8..e51236be2ffb6 100644 --- a/packages/SystemUI/tests/AndroidManifest.xml +++ b/packages/SystemUI/tests/AndroidManifest.xml @@ -106,6 +106,12 @@ android:finishOnCloseSystemDialogs="true" android:excludeFromRecents="true" /> + + + assertThat(activity.isFinishing).isFalse() + + activity.onBackPressed() + + assertThat(activity.isFinishing).isTrue() + } + } +}