Skip to content

Commit

Permalink
Fix #5404: Migrate away from onBackPressed for remaining activities (#…
Browse files Browse the repository at this point in the history
…5526)

<!-- READ ME FIRST: Please fill in the explanation section below and
check off every point from the Essential Checklist! -->
## Explanation
<!--
- Explain what your PR does. If this PR fixes an existing bug, please
include
- "Fixes #bugnum:" in the explanation so that GitHub can auto-close the
issue
  - when this PR is merged.
  -->
 Fixes #5404 

This PR migrates deprecated `onBackPressed` usage to
`OnBackPressedDispatcher` callback in the following activities and
presenters.

- ProfileEditActivity
- ProfileEditActivityPresenter
- QuestionPlayerActivityPresenter
- WalkthroughFinalFragmentPresenter

## Essential Checklist
<!-- Please tick the relevant boxes by putting an "x" in them. -->
- [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)).

## For UI-specific PRs only
<!-- Delete these section if this PR does not include UI-related
changes. -->
If your PR includes UI-related changes, then:
- Add screenshots for portrait/landscape for both a tablet & phone of
the before & after UI changes
- For the screenshots above, include both English and pseudo-localized
(RTL) screenshots (see [RTL
guide](https://github.com/oppia/oppia-android/wiki/RTL-Guidelines))
- Add a video showing the full UX flow with a screen reader enabled (see
[accessibility
guide](https://github.com/oppia/oppia-android/wiki/Accessibility-A11y-Guide))
- For PRs introducing new UI elements or color changes, both light and
dark mode screenshots must be included
- Add a screenshot demonstrating that you ran affected Espresso tests
locally & that they're passing

---------

Co-authored-by: Adhiambo Peres <59600948+adhiamboperes@users.noreply.github.com>
Co-authored-by: Mr. 17 <saptakmanna100@gmail.com>
  • Loading branch information
3 people authored Oct 3, 2024
1 parent 8d44bf7 commit 20c4cad
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.oppia.android.app.settings.profile
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
import org.oppia.android.app.model.ProfileEditActivityParams
Expand Down Expand Up @@ -43,17 +44,25 @@ class ProfileEditActivity : InjectableAutoLocalizedAppCompatActivity() {
super.onCreate(savedInstanceState)
(activityComponent as ActivityComponentImpl).inject(this)
profileEditActivityPresenter.handleOnCreate()

onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(/* enabled = */ true) {
override fun handleOnBackPressed() {
this@ProfileEditActivity.handleBackPress()
}
}
)
}

override fun onBackPressed() {
private fun handleBackPress() {
val args = intent.getProtoExtra(
PROFILE_EDIT_ACTIVITY_PARAMS_KEY,
ProfileEditActivityParams.getDefaultInstance()
)
val isMultipane = args?.isMultipane ?: false
if (isMultipane) {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
super.onBackPressed()
finish()
} else {
val intent = Intent(this, ProfileListActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class ProfileEditActivityPresenter @Inject constructor(

toolbar.setNavigationOnClickListener {
if (isMultipane) {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
activity.onBackPressed()
activity.onBackPressedDispatcher.onBackPressed()
} else {
val intent = Intent(activity, ProfileListActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.oppia.android.app.topic.questionplayer
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
import org.oppia.android.app.hintsandsolution.HintsAndSolutionListener
Expand Down Expand Up @@ -56,11 +57,16 @@ class QuestionPlayerActivity :
val profileId =
intent.extractCurrentUserProfileId()
questionPlayerActivityPresenter.handleOnCreate(profileId)
}

override fun onBackPressed() {
showStopExplorationDialogFragment()
questionPlayerActivityPresenter.setReadingTextSizeNormal()
onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(/* enabled = */ true) {
override fun handleOnBackPressed() {
showStopExplorationDialogFragment()
questionPlayerActivityPresenter.setReadingTextSizeNormal()
}
}
)
}

override fun restartSession() = questionPlayerActivityPresenter.restartSession()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class QuestionPlayerActivityPresenter @Inject constructor(
activity.setSupportActionBar(binding.questionPlayerToolbar)

binding.questionPlayerToolbar.setNavigationOnClickListener {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
activity.onBackPressed()
activity.onBackPressedDispatcher.onBackPressed()
}

retrieveReadingTextSize().observe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.oppia.android.app.walkthrough
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
import org.oppia.android.app.model.ProfileId
Expand All @@ -22,6 +23,15 @@ class WalkthroughActivity :
super.onCreate(savedInstanceState)
(activityComponent as ActivityComponentImpl).inject(this)
walkthroughActivityPresenter.handleOnCreate()

onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(/* enabled = */ true) {
override fun handleOnBackPressed() {
walkthroughActivityPresenter.handleSystemBack()
}
}
)
}

override fun currentPage(walkthroughPage: Int) {
Expand All @@ -33,10 +43,6 @@ class WalkthroughActivity :
walkthroughActivityPresenter.changePage(walkthroughPage)
}

override fun onBackPressed() {
walkthroughActivityPresenter.handleSystemBack()
}

companion object {

fun createWalkthroughActivityIntent(context: Context, internalProfileId: Int): Intent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class WalkthroughFinalFragmentPresenter @Inject constructor(
}

override fun goBack() {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
activity.onBackPressed()
activity.onBackPressedDispatcher.onBackPressed()
}
}

0 comments on commit 20c4cad

Please sign in to comment.