Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5404 : Migrate onBackPressed #5427

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.oppia.android.app.administratorcontrols
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import org.oppia.android.R
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
Expand Down Expand Up @@ -83,6 +84,33 @@ class AdministratorControlsActivity :
isProfileDeletionDialogVisible
)
title = resourceHandler.getStringInLocale(R.string.administrator_controls)
handleBackPress()
}

private fun handleBackPress() {
onBackPressedDispatcher.addCallback(
this@AdministratorControlsActivity,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
val fragment =
supportFragmentManager.findFragmentById(
R.id.administrator_controls_fragment_multipane_placeholder
)
/*
* If the current fragment is ProfileListFragment then the activity should end on back press.
* If it's instead ProfileEditFragment then profileListFragment should be inflated via
* handleOnBackPressed.
*/
if (fragment is ProfileEditFragment) {
administratorControlsActivityPresenter.handleOnBackPressed()
} else {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
isEnabled = true
}
}
}
)
}

override fun routeToAppVersion() {
Expand Down Expand Up @@ -121,24 +149,6 @@ class AdministratorControlsActivity :
}
}

override fun onBackPressed() {
val fragment =
supportFragmentManager.findFragmentById(
R.id.administrator_controls_fragment_multipane_placeholder
)
/*
* If the current fragment is ProfileListFragment then the activity should end on back press.
* If it's instead ProfileEditFragment then profileListFragment should be inflated via
* handleOnBackPressed.
*/
if (fragment is ProfileEditFragment) {
administratorControlsActivityPresenter.handleOnBackPressed()
} else {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
super.onBackPressed()
}
}

override fun loadProfileList() {
lastLoadedFragment = PROFILE_LIST_FRAGMENT
administratorControlsActivityPresenter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
import org.oppia.android.app.model.ScreenName.APP_VERSION_ACTIVITY
Expand All @@ -19,16 +20,29 @@ class AppVersionActivity : InjectableAutoLocalizedAppCompatActivity() {
super.onCreate(savedInstanceState)
(activityComponent as ActivityComponentImpl).inject(this)
appVersionActivityPresenter.handleOnCreate()
handleBackPress()
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
return super.onOptionsItemSelected(item)
}

private fun handleBackPress() {
onBackPressedDispatcher.addCallback(
this@AppVersionActivity,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
isEnabled = true
}
}
)
}

companion object {
/** Returns an [Intent] to start this activity. */
fun createAppVersionActivityIntent(context: Context): Intent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
import org.oppia.android.app.model.ScreenName.PROFILE_AND_DEVICE_ID_ACTIVITY
Expand All @@ -24,16 +25,28 @@ class ProfileAndDeviceIdActivity : InjectableAutoLocalizedAppCompatActivity() {
super.onCreate(savedInstanceState)
(activityComponent as ActivityComponentImpl).inject(this)
profileAndDeviceIdActivityPresenter.handleOnCreate()
handleBackPress()
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
return super.onOptionsItemSelected(item)
}

private fun handleBackPress() {
onBackPressedDispatcher.addCallback(
this@ProfileAndDeviceIdActivity,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
isEnabled = true
}
}
)
}
companion object {
/** Returns an [Intent] to launch [ProfileAndDeviceIdActivity]. */
fun createIntent(context: Context): Intent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import org.oppia.android.R
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
Expand Down Expand Up @@ -37,12 +38,25 @@ class MarkChaptersCompletedActivity : InjectableAutoLocalizedAppCompatActivity()
val showConfirmationNotice = args?.showConfirmationNotice ?: false
markChaptersCompletedActivityPresenter.handleOnCreate(internalProfileId, showConfirmationNotice)
title = resourceHandler.getStringInLocale(R.string.mark_chapters_completed_activity_title)
handleBackPress()
}

private fun handleBackPress() {
onBackPressedDispatcher.addCallback(
this@MarkChaptersCompletedActivity,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
isEnabled = true
}
}
)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
return super.onOptionsItemSelected(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import org.oppia.android.R
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
Expand Down Expand Up @@ -34,12 +35,25 @@ class MarkStoriesCompletedActivity : InjectableAutoLocalizedAppCompatActivity()
internalProfileId = profileId?.internalId ?: -1
markStoriesCompletedActivityPresenter.handleOnCreate(internalProfileId)
title = resourceHandler.getStringInLocale(R.string.mark_stories_completed_activity_title)
handleBackPress()
}

private fun handleBackPress() {
onBackPressedDispatcher.addCallback(
this@MarkStoriesCompletedActivity,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
isEnabled = true
}
}
)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
return super.onOptionsItemSelected(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import org.oppia.android.R
import org.oppia.android.app.activity.ActivityComponentImpl
import org.oppia.android.app.activity.InjectableAutoLocalizedAppCompatActivity
Expand Down Expand Up @@ -34,12 +35,25 @@ class MarkTopicsCompletedActivity : InjectableAutoLocalizedAppCompatActivity() {
internalProfileId = profileId?.internalId ?: -1
markTopicsCompletedActivityPresenter.handleOnCreate(internalProfileId)
title = resourceHandler.getStringInLocale(R.string.mark_topics_completed_activity_title)
handleBackPress()
}

private fun handleBackPress() {
onBackPressedDispatcher.addCallback(
this@MarkTopicsCompletedActivity,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
isEnabled = true
}
}
)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
return super.onOptionsItemSelected(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.oppia.android.app.options
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.ReadingTextSize
Expand Down Expand Up @@ -34,6 +35,18 @@ class ReadingTextSizeActivity : InjectableAutoLocalizedAppCompatActivity() {
savedInstanceState?.retrieveStateBundle()?.selectedReadingTextSize
?: retrieveActivityParams().readingTextSize
readingTextSizeActivityPresenter.handleOnCreate(readingTextSize)
handleBackPress()
}

private fun handleBackPress() {
onBackPressedDispatcher.addCallback(
this@ReadingTextSizeActivity,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
onBackPressedDispatcher.onBackPressed()
}
}
)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class ReadingTextSizeActivityPresenter @Inject constructor(
private fun setToolbar() {
val readingTextSizeToolbar: Toolbar = activity.findViewById(R.id.reading_text_size_toolbar)
readingTextSizeToolbar.setNavigationOnClickListener {
@Suppress("DEPRECATION") // TODO(#5404): Migrate to a back pressed dispatcher.
activity.onBackPressed()
activity.onBackPressedDispatcher.onBackPressed()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.oppia.android.app.player.exploration
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.HintsAndSolutionDialogFragment
Expand Down Expand Up @@ -66,6 +67,20 @@ class ExplorationActivity :
params.parentScreen,
params.isCheckpointingEnabled
)
handleBackPress()
}

private fun handleBackPress() {
onBackPressedDispatcher.addCallback(
this@ExplorationActivity,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
isEnabled = true
onBackPressedDispatcher.onBackPressed()
isEnabled = false
}
}
)
}

// TODO(#1655): Re-restrict access to fields in tests post-Gradle.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ class ExplorationActivityPresenter @Inject constructor(
}

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

binding.actionAudioPlayer.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.oppia.android.app.resumelesson
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.home.RouteToExplorationListener
Expand Down Expand Up @@ -37,6 +38,20 @@ class ResumeLessonActivity :
params.parentScreen,
params.checkpoint
)
handleBackPress()
}

private fun handleBackPress() {
onBackPressedDispatcher.addCallback(
this@ResumeLessonActivity,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
isEnabled = true
}
}
)
}

// TODO(#1655): Re-restrict access to fields in tests post-Gradle.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ class ResumeLessonActivityPresenter @Inject constructor(
activity.setSupportActionBar(resumeLessonToolbar)

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

if (getResumeLessonFragment() == null) {
Expand Down
Loading
Loading