android: Fix setup rotation bug

If you rotated the device at the "Add Games" screen the buttons would disappear until you trigged them from the beginning page swap. Now button state is saved across recreation.
This commit is contained in:
Charles Lombardo 2023-04-26 01:52:26 -04:00 committed by bunnei
parent 61e0042633
commit 48c506682d
2 changed files with 26 additions and 4 deletions

View file

@ -13,6 +13,8 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.navigation.findNavController import androidx.navigation.findNavController
@ -36,6 +38,11 @@ class SetupFragment : Fragment() {
private lateinit var mainActivity: MainActivity private lateinit var mainActivity: MainActivity
companion object {
const val KEY_NEXT_VISIBILITY = "NextButtonVisibility"
const val KEY_BACK_VISIBILITY = "BackButtonVisibility"
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
exitTransition = MaterialFadeThrough() exitTransition = MaterialFadeThrough()
@ -134,14 +141,27 @@ class SetupFragment : Fragment() {
binding.buttonNext.setOnClickListener { pageForward() } binding.buttonNext.setOnClickListener { pageForward() }
binding.buttonBack.setOnClickListener { pageBackward() } binding.buttonBack.setOnClickListener { pageBackward() }
if (binding.viewPager2.currentItem == 0) { if (savedInstanceState != null) {
binding.buttonNext.visibility = View.INVISIBLE val nextIsVisible = savedInstanceState.getBoolean(KEY_NEXT_VISIBILITY)
binding.buttonBack.visibility = View.INVISIBLE val backIsVisible = savedInstanceState.getBoolean(KEY_BACK_VISIBILITY)
if (nextIsVisible) {
binding.buttonNext.visibility = View.VISIBLE
}
if (backIsVisible) {
binding.buttonBack.visibility = View.VISIBLE
}
} }
setInsets() setInsets()
} }
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean(KEY_NEXT_VISIBILITY, binding.buttonNext.isVisible)
outState.putBoolean(KEY_BACK_VISIBILITY, binding.buttonBack.isVisible)
}
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
_binding = null _binding = null
@ -190,7 +210,7 @@ class SetupFragment : Fragment() {
} }
private fun setInsets() = private fun setInsets() =
ViewCompat.setOnApplyWindowInsetsListener(binding.setupRoot) { view: View, windowInsets: WindowInsetsCompat -> ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view: View, windowInsets: WindowInsetsCompat ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
view.setPadding( view.setPadding(
insets.left, insets.left,

View file

@ -22,6 +22,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="16dp" android:layout_margin="16dp"
android:text="@string/next" android:text="@string/next"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" /> app:layout_constraintEnd_toEndOf="parent" />
@ -32,6 +33,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="16dp" android:layout_margin="16dp"
android:text="@string/back" android:text="@string/back"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" /> app:layout_constraintStart_toStartOf="parent" />