diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/SingleFragmentActivity.kt b/AnkiDroid/src/main/java/com/ichi2/anki/SingleFragmentActivity.kt index 7268d43c841e..1e643896e1da 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/SingleFragmentActivity.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/SingleFragmentActivity.kt @@ -19,11 +19,15 @@ import android.content.Context import android.content.Intent import android.os.Bundle import android.view.KeyEvent +import androidx.fragment.app.DialogFragment import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentContainerView import androidx.fragment.app.commit import com.ichi2.anki.android.input.ShortcutGroup import com.ichi2.anki.android.input.ShortcutGroupProvider +import com.ichi2.anki.dialogs.customstudy.CustomStudyDialog +import com.ichi2.anki.dialogs.customstudy.CustomStudyDialogFactory +import com.ichi2.utils.ExtendedFragmentFactory import com.ichi2.utils.getInstanceFromClassName import timber.log.Timber import kotlin.reflect.KClass @@ -38,7 +42,7 @@ import kotlin.reflect.jvm.jvmName * * [getIntent] can be used as an easy way to build a [SingleFragmentActivity] */ -open class SingleFragmentActivity : AnkiActivity() { +open class SingleFragmentActivity : AnkiActivity(), CustomStudyDialog.CustomStudyListener { /** The displayed fragment. */ lateinit var fragment: Fragment @@ -46,6 +50,13 @@ open class SingleFragmentActivity : AnkiActivity() { if (showedActivityFailedScreen(savedInstanceState)) { return } + + // This page *may* host the CustomStudyDialog (CongratsPage) + // CustomStudyDialog requires a custom factory install during lifecycle or it can + // crash during lifecycle resume after background kill + val customStudyDialogFactory = CustomStudyDialogFactory({ this.getColUnsafe }, this) + customStudyDialogFactory.attachToActivity(this) + super.onCreate(savedInstanceState) if (!ensureStoragePermissions()) { return @@ -101,6 +112,41 @@ open class SingleFragmentActivity : AnkiActivity() { override val shortcuts: ShortcutGroup? get() = (fragment as? ShortcutGroupProvider)?.shortcuts + + // Begin - implementation of CustomStudyListener methods here for crash fix + // TODO - refactor https://github.com/ankidroid/Anki-Android/pull/17508#pullrequestreview-2465561993 + private fun openStudyOptionsAndFinish() { + val intent = Intent(this, StudyOptionsActivity::class.java).apply { + putExtra("withDeckOptions", false) + } + startActivity(intent, null) + this.finish() + } + + override fun onExtendStudyLimits() { + Timber.v("CustomStudyListener::hideProgressBar() - not handled") + openStudyOptionsAndFinish() + } + + override fun showDialogFragment(newFragment: DialogFragment) { + Timber.v("CustomStudyListener::showDialogFragment()") + newFragment.show(supportFragmentManager, null) + } + + override fun startActivity(intent: Intent) { + Timber.v("CustomStudyListener::startActivity() - not handled") + } + + override fun onCreateCustomStudySession() { + Timber.v("CustomStudyListener::onCreateCustomStudySession()") + openStudyOptionsAndFinish() + } + + override fun hideProgressBar() { + Timber.v("CustomStudyListener::hideProgressBar() - not handled") + } + + // END CustomStudyListener temporary implementation - should refactor out } interface DispatchKeyEventListener {