From 6afcfd7cbd2f20089ed7963cc6eb333af42d37b3 Mon Sep 17 00:00:00 2001 From: Brian Isganitis Date: Wed, 26 May 2021 20:43:15 -0400 Subject: [PATCH] Show toolbar back button on subsettings pages Test: Back button displayed and functional on subsettings Bug: 187732263 Change-Id: I6a3679de3a00480f5a0861f966d178be25f001d4 --- .../launcher3/settings/SettingsActivityTest.java | 15 +++++++++++++++ .../launcher3/settings/SettingsActivity.java | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/robolectric_tests/src/com/android/launcher3/settings/SettingsActivityTest.java b/robolectric_tests/src/com/android/launcher3/settings/SettingsActivityTest.java index 85bf28e0b9..3271812b45 100644 --- a/robolectric_tests/src/com/android/launcher3/settings/SettingsActivityTest.java +++ b/robolectric_tests/src/com/android/launcher3/settings/SettingsActivityTest.java @@ -27,6 +27,7 @@ import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent; import static androidx.test.espresso.intent.matcher.IntentMatchers.hasExtra; import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant; import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; +import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription; import static androidx.test.espresso.matcher.ViewMatchers.withId; import static androidx.test.espresso.matcher.ViewMatchers.withText; @@ -109,6 +110,7 @@ public class SettingsActivityTest { onView(withText("About")).check(matches(isDisplayed())); onView(withText("Version")).check(matches(isDisplayed())); + onView(withContentDescription("Navigate up")).check(matches(isDisplayed())); } @Test @@ -119,6 +121,7 @@ public class SettingsActivityTest { onView(withText("Developer Options")).check(matches(isDisplayed())); onView(withId(R.id.filter_box)).check(matches(isDisplayed())); + onView(withContentDescription("Navigate up")).check(matches(isDisplayed())); } @Test @@ -134,4 +137,16 @@ public class SettingsActivityTest { assertThat(e.getMessage()).contains(fragmentClass); } } + + @Test + public void testSettings_backButtonFinishesActivity() { + Bundle fragmentArgs = new Bundle(); + fragmentArgs.putString(ARG_PREFERENCE_ROOT, "about_screen"); + Intent intent = new Intent(mApplicationContext, SettingsActivity.class) + .putExtra(EXTRA_FRAGMENT_ARGS, fragmentArgs); + ActivityScenario scenario = ActivityScenario.launch(intent); + + onView(withContentDescription("Navigate up")).perform(click()); + scenario.onActivity(activity -> assertThat(activity.isFinishing()).isTrue()); + } } diff --git a/src/com/android/launcher3/settings/SettingsActivity.java b/src/com/android/launcher3/settings/SettingsActivity.java index d754ed2ff2..915e140aaf 100644 --- a/src/com/android/launcher3/settings/SettingsActivity.java +++ b/src/com/android/launcher3/settings/SettingsActivity.java @@ -24,6 +24,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; +import android.view.MenuItem; import android.view.View; import androidx.annotation.NonNull; @@ -86,8 +87,12 @@ public class SettingsActivity extends FragmentActivity setActionBar(findViewById(R.id.action_bar)); WindowCompat.setDecorFitsSystemWindows(getWindow(), false); + Intent intent = getIntent(); + if (intent.hasExtra(EXTRA_FRAGMENT) || intent.hasExtra(EXTRA_FRAGMENT_ARGS)) { + getActionBar().setDisplayHomeAsUpEnabled(true); + } + if (savedInstanceState == null) { - Intent intent = getIntent(); Bundle args = intent.getBundleExtra(EXTRA_FRAGMENT_ARGS); if (args == null) { args = new Bundle(); @@ -164,6 +169,15 @@ public class SettingsActivity extends FragmentActivity return startPreference(getString(R.string.settings_fragment_name), args, pref.getKey()); } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + onBackPressed(); + return true; + } + return super.onOptionsItemSelected(item); + } + /** * This fragment shows the launcher preferences. */