Skip to content

Commit 3010482

Browse files
lukstbitmikehardy
authored andcommitted
Fix background color update for TemplatePreviewerFragment in CardTemplateEditor
The fragment's view was accessed to set the background on it but it wasn't available at that moment resulting in an IllegalStateException. The fragment's view wasn't available because although the transaction that adds the fragment is done synchronously it only takes the fragment to the parent's CURRENT lifecycle state(still too early in the lifecycle).
1 parent 3dca59d commit 3010482

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,11 @@ open class CardTemplateEditor : AnkiActivity(), DeckSelectionListener {
203203
tags = note.tags,
204204
fillEmpty = true
205205
)
206-
val fragment = TemplatePreviewerFragment.newInstance(args)
206+
val backgroundColor = ThemeUtils.getThemeAttrColor(this@CardTemplateEditor, R.attr.alternativeBackgroundColor)
207+
val fragment = TemplatePreviewerFragment.newInstance(args, backgroundColor)
207208
supportFragmentManager.commitNow {
208209
replace(R.id.fragment_container, fragment)
209210
}
210-
val backgroundColor = ThemeUtils.getThemeAttrColor(this@CardTemplateEditor, R.attr.alternativeBackgroundColor)
211-
fragment.requireView().setBackgroundColor(backgroundColor)
212211
}
213212
}
214213

AnkiDroid/src/main/java/com/ichi2/anki/previewer/TemplatePreviewerFragment.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.ichi2.anki.cardviewer.CardMediaPlayer
2929
import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider
3030
import com.ichi2.anki.snackbar.SnackbarBuilder
3131
import com.ichi2.anki.utils.ext.sharedPrefs
32+
import com.ichi2.utils.BundleUtils.getNullableInt
3233
import kotlinx.coroutines.flow.launchIn
3334
import kotlinx.coroutines.flow.onEach
3435

@@ -65,14 +66,28 @@ class TemplatePreviewerFragment :
6566
if (sharedPrefs().getBoolean("safeDisplay", false)) {
6667
view.findViewById<MaterialCardView>(R.id.webview_container).elevation = 0F
6768
}
69+
70+
arguments?.getNullableInt(ARG_BACKGROUND_OVERRIDE_COLOR)?.let { color ->
71+
view.setBackgroundColor(color)
72+
}
6873
}
6974

7075
companion object {
7176
const val ARGS_KEY = "templatePreviewerArgs"
77+
private const val ARG_BACKGROUND_OVERRIDE_COLOR = "arg_background_override_color"
7278

73-
fun newInstance(arguments: TemplatePreviewerArguments): TemplatePreviewerFragment {
79+
/**
80+
* @param backgroundOverrideColor optional color to be used as background on the root view
81+
* of this fragment
82+
*/
83+
fun newInstance(
84+
arguments: TemplatePreviewerArguments,
85+
backgroundOverrideColor: Int? = null
86+
): TemplatePreviewerFragment {
7487
return TemplatePreviewerFragment().apply {
75-
this.arguments = bundleOf(ARGS_KEY to arguments)
88+
val args = bundleOf(ARGS_KEY to arguments)
89+
backgroundOverrideColor?.let { args.putInt(ARG_BACKGROUND_OVERRIDE_COLOR, backgroundOverrideColor) }
90+
this.arguments = args
7691
}
7792
}
7893
}

0 commit comments

Comments
 (0)