My app was saving a bigger bundle in activity onSaveInstanceState() each time the screen was rotated, and I came across something really confusing. Here is a minimal example:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
val rcs = outState.getIntegerArrayList("KEY_COMPONENT_ACTIVITY_REGISTERED_RCS")
val keys = outState.getStringArrayList("KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS")
Log.d("MainActivity", "${rcs?.size}: $rcs, $keys")
}
}
I get the following output each time the screen is rotated:
3: [1332505437, 1835553837, 670316111], [FragmentManager:StartIntentSenderForResult, ...
6: [1332505437, 91080073, 1835553837, 381123153, 1187376284, 670316111], ...
...
See the full log after 6 rotations: https://pastebin.com/yfE04Fmc
Each time the screen is rotated, 3 elements are added to these two entries. After rotating the screen for a while, the instance state becomes significantly bigger for no apparent reason.
Target API: 30
Tested on: API 28 and 30
Does anyone know why this is happening?
UPDATE: I want to clarify that I do not save any state into the Bundle. The values I show are created by Android itself: in ActivityResultRegistry. I've created an empty project for this example, and the code I provided is the only code I have - there is nothing else that interacts with the state Bundle.
UPDATE 2: Bug report submitted: https://issuetracker.google.com/issues/191893160
UPDATE 3: Bug fixed, will be a part of Activity 1.3.0-rc02 and 1.2.4 releases