If we change the accessibility font from big to small or small to big and come back to the app, the views' visibility are not preserved; which means if a view is invisible or gone in the app and the font size is changed in accessibility and come back back to the app, we will see that view visible.
Example,
class MainActivity : AppCompatActivity(R.layout.activity_main) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
findViewById<Button>(R.id.b3).setOnClickListener {
findViewById<Button>(R.id.b2).visibility = GONE
}
}
}
The layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@ id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="something" />
<Button
android:id="@ id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="something big" />
<Button
android:id="@ id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@ id/layout" />
</LinearLayout>
Now, click on the "click" button and go to setting->accessibility->font->CHANGE and come back the app. The hidden button is visible again.
Is there any workaround other than keeping the values in other variables and set that later?
CodePudding user response:
Now, click on the "click" button and go to setting->accessibility->font->CHANGE and come back the app. The hidden button is visible again.
You are undergoing a configuration change. There are lots and lots of configuration changes, such as:
- Orientation change (for apps that support portrait and landscape)
- Enable/disable dark mode
- Locale/language change
- Window resizes (for split-screen and freeform multi-window environments)
- Font scale change or density scale ("screen zoom") change
- And so on
Is there any workaround other than keeping the values in other variables and set that later?
Generally, that will not work either. Your existing activity is being destroyed and recreated. Any fields in the old activity go away when the old activity does.
The documentation covers configuration changes, as will any decent book or course on Android app development. Your primary options are:
Use the saved instance state
Bundle
or aViewModel
to hold data that you can then use in the new activity to restore your UI to whatever state it should be in after the configuration change; orOpt out of the configuration changes and handle all of the UI updates yourself (e.g., set all of your displayed text to the new language for a locale change)
The former is the typical approach for classic View
-based UI development, as you are doing. The latter is gaining popularity with Jetpack Compose, which handles configuration changes as part of its existing recompostion system.