'Android 12 (API 31) - Fragment not being destroyed when going in background

I'm having this issue with my fragment only in API 31. In the host Activity I use replace to add my fragment:

transaction.replace(container, fragment, tag);

Everything runs as expected. But when I send the app in background in API 31 the fragment's lifecycle doesn't evolve as I was thinking. It doesn't call onDestroy. It only goes till onStop. Meanwhile, in previous versions of Android the fragment gets completely destroyed when I send the app to background.

Anyone else experiencing this problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source