From 9d4edf047c27e0a474ba86aee5769c3e0c12b6fc Mon Sep 17 00:00:00 2001 From: Ayush Yadav <143514610+theayushyadav11@users.noreply.github.com> Date: Fri, 15 Nov 2024 14:23:57 +0530 Subject: [PATCH] Fixes #3641 Use finish() instead of intent for smoother navigation (#5557) ## Description: - Fixes #3641 This PR simplifies the navigation logic in `AddProfileActivity` and documents the reasoning behind not using `finish()` in `ExitProfileDialogFragment`. ### Changes Made: - **AddProfileActivity:** - Replaced the use of `Intent` with `FLAG_ACTIVITY_CLEAR_TOP` by calling `finish()`. - With the live data bug resolved, the activity stack can now be managed more cleanly without requiring a new intent. **Benefits:** - Avoids unnecessary activity recreation. - Improves navigation efficiency and reduces overhead. - **ExitProfileDialogFragment:** - Retained the use of an `Intent` to navigate to `ProfileChooserActivity` instead of switching to `finish()`. - **Reason:** - `ProfileChooserActivity` is not directly below the current activity in the stack. - If `finish()` were used, the app would incorrectly navigate to `PinPasswordActivity`, resulting in undesirable behavior. ### Testing: 1. Verified that `AddProfileActivity` correctly navigates back to `ProfileChooserActivity` using `finish()` without activity recreation. 2. Confirmed that `ExitProfileDialogFragment` behaves as expected, retaining the correct navigation path through `Intent` to `ProfileChooserActivity`. ### Summary: This PR ensures cleaner navigation for `AddProfileActivity` by using `finish()`, while maintaining correct behavior in `ExitProfileDialogFragment` by continuing to use an intent-based approach. This makes the app more efficient while preserving user experience. ### Video Demo as everything is working fine. https://github.com/user-attachments/assets/fb383845-5672-4236-8618-a87583cbbba0 ## Essential Checklist - [x] The PR title and explanation each start with "Fix #bugnum: " (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".) - [x] Any changes to [scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets) files have their rationale included in the PR explanation. - [x] The PR follows the [style guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide). - [x] The PR does not contain any unnecessary code changes from Android Studio ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)). - [x] The PR is made from a branch that's **not** called "develop" and is up-to-date with "develop". - [x] The PR is **assigned** to the appropriate reviewers ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)). --- .../android/app/drawer/ExitProfileDialogFragment.kt | 1 - .../oppia/android/app/profile/AddProfileActivity.kt | 5 +---- .../android/app/profile/AddProfileActivityTest.kt | 10 ++++++++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/oppia/android/app/drawer/ExitProfileDialogFragment.kt b/app/src/main/java/org/oppia/android/app/drawer/ExitProfileDialogFragment.kt index 7900163e9a5..2dfdd918fc7 100644 --- a/app/src/main/java/org/oppia/android/app/drawer/ExitProfileDialogFragment.kt +++ b/app/src/main/java/org/oppia/android/app/drawer/ExitProfileDialogFragment.kt @@ -70,7 +70,6 @@ class ExitProfileDialogFragment : InjectableDialogFragment() { dialog.dismiss() } .setPositiveButton(R.string.home_activity_back_dialog_exit) { _, _ -> - // TODO(#3641): Investigate on using finish instead of intent. val intent = ProfileChooserActivity.createProfileChooserActivity(activity!!) if (!restoreLastCheckedItem) { intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) diff --git a/app/src/main/java/org/oppia/android/app/profile/AddProfileActivity.kt b/app/src/main/java/org/oppia/android/app/profile/AddProfileActivity.kt index 1acff5fd581..c7d0e60857c 100644 --- a/app/src/main/java/org/oppia/android/app/profile/AddProfileActivity.kt +++ b/app/src/main/java/org/oppia/android/app/profile/AddProfileActivity.kt @@ -44,10 +44,7 @@ class AddProfileActivity : InjectableAutoLocalizedAppCompatActivity() { } override fun onSupportNavigateUp(): Boolean { - // TODO(#3641): Investigate on using finish instead of intent. - val intent = Intent(this, ProfileChooserActivity::class.java) - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - startActivity(intent) + finish() return false } diff --git a/app/src/sharedTest/java/org/oppia/android/app/profile/AddProfileActivityTest.kt b/app/src/sharedTest/java/org/oppia/android/app/profile/AddProfileActivityTest.kt index 0152d1056d7..b9aa2114cd6 100644 --- a/app/src/sharedTest/java/org/oppia/android/app/profile/AddProfileActivityTest.kt +++ b/app/src/sharedTest/java/org/oppia/android/app/profile/AddProfileActivityTest.kt @@ -13,6 +13,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.test.core.app.ActivityScenario.launch import androidx.test.core.app.ApplicationProvider import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.action.ViewActions.closeSoftKeyboard import androidx.test.espresso.action.ViewActions.pressImeActionButton @@ -1748,6 +1749,15 @@ class AddProfileActivityTest { assertThat(currentScreenName).isEqualTo(ScreenName.ADD_PROFILE_ACTIVITY) } + @Test + fun testAddProfileActivity_onBackPressed_finishActivity() { + val scenario = launch(AddProfileActivity::class.java) + onView(isRoot()).perform(ViewActions.pressBack()) + testCoroutineDispatchers.runCurrent() + scenario.onActivity { activity -> + assertThat(activity.isFinishing).isTrue() + } + } private fun createAddProfileActivityIntent(): Intent { return AddProfileActivity.createAddProfileActivityIntent( ApplicationProvider.getApplicationContext(),