detail: fix state saving crash
This commit is contained in:
@@ -53,9 +53,13 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
|
|||||||
override val playbackModel: PlaybackViewModel by activityViewModels()
|
override val playbackModel: PlaybackViewModel by activityViewModels()
|
||||||
|
|
||||||
private var spacingSmall = 0
|
private var spacingSmall = 0
|
||||||
|
private var lastHadDualPane = false
|
||||||
|
private var lastWasScrolled = false
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
lastHadDualPane = savedInstanceState?.getBoolean(KEY_DUAL_PANE) ?: false
|
||||||
|
lastWasScrolled = savedInstanceState?.getBoolean(KEY_SCROLLED) ?: false
|
||||||
// Detail transitions are always on the X axis. Shared element transitions are more
|
// Detail transitions are always on the X axis. Shared element transitions are more
|
||||||
// semantically correct, but are also too buggy to be sensible.
|
// semantically correct, but are also too buggy to be sensible.
|
||||||
enterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, true)
|
enterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, true)
|
||||||
@@ -75,6 +79,10 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
|
|||||||
|
|
||||||
override fun onBindingCreated(binding: FragmentDetailBinding, savedInstanceState: Bundle?) {
|
override fun onBindingCreated(binding: FragmentDetailBinding, savedInstanceState: Bundle?) {
|
||||||
super.onBindingCreated(binding, savedInstanceState)
|
super.onBindingCreated(binding, savedInstanceState)
|
||||||
|
// The straightforward "lets save the instance state from the view state"
|
||||||
|
// method won't work due to android lifecycle restrictions. Populate in-memory
|
||||||
|
// state as a backup for the saved instance state bundle.
|
||||||
|
saveStateInMemory(binding)
|
||||||
|
|
||||||
// --- UI SETUP ---
|
// --- UI SETUP ---
|
||||||
binding.detailAppbar?.addOnOffsetChangedListener(this)
|
binding.detailAppbar?.addOnOffsetChangedListener(this)
|
||||||
@@ -118,6 +126,8 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroyBinding(binding: FragmentDetailBinding) {
|
override fun onDestroyBinding(binding: FragmentDetailBinding) {
|
||||||
|
// see onBindingCreated
|
||||||
|
saveStateInMemory(binding)
|
||||||
super.onDestroyBinding(binding)
|
super.onDestroyBinding(binding)
|
||||||
binding.detailAppbar?.removeOnOffsetChangedListener(this)
|
binding.detailAppbar?.removeOnOffsetChangedListener(this)
|
||||||
binding.detailNormalToolbar.setOnMenuItemClickListener(null)
|
binding.detailNormalToolbar.setOnMenuItemClickListener(null)
|
||||||
@@ -126,9 +136,11 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
|
|||||||
|
|
||||||
override fun onSaveInstanceState(outState: Bundle) {
|
override fun onSaveInstanceState(outState: Bundle) {
|
||||||
super.onSaveInstanceState(outState)
|
super.onSaveInstanceState(outState)
|
||||||
val binding = requireBinding()
|
// Update in-memory saved state if we can (binding may be null) and then
|
||||||
outState.putBoolean(KEY_DUAL_PANE, binding.detailPane != null)
|
// save the instance state from that
|
||||||
outState.putBoolean(KEY_SCROLLED, binding.detailRecycler.computeVerticalScrollOffset() > 0f)
|
binding?.let(::saveStateInMemory)
|
||||||
|
outState.putBoolean(KEY_DUAL_PANE, lastHadDualPane)
|
||||||
|
outState.putBoolean(KEY_SCROLLED, lastWasScrolled)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {
|
override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {
|
||||||
@@ -215,6 +227,11 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
|
|||||||
view.translationY = spacingSmall * (1 - ratio)
|
view.translationY = spacingSmall * (1 - ratio)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun saveStateInMemory(binding: FragmentDetailBinding) {
|
||||||
|
lastHadDualPane = binding.detailPane != null
|
||||||
|
lastWasScrolled = binding.detailRecycler.computeVerticalScrollOffset() > 0f
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract fun onPlayParent(parent: P)
|
protected abstract fun onPlayParent(parent: P)
|
||||||
|
|
||||||
protected abstract fun onShuffleParent(parent: P)
|
protected abstract fun onShuffleParent(parent: P)
|
||||||
|
|||||||
Reference in New Issue
Block a user