Android: IllegalStateException: onAttach called multiple times with different hilt context | by Gandikota Venkata Ravi Kumar | Feb, 2025

Android: IllegalStateException: onAttach called multiple times with different hilt context | by Gandikota Venkata Ravi Kumar | Feb, 2025

I would like to share my experience with a crash in my android app only for Samsung devices. The crash log says like this

from Android studio logcat

It is clear that the onAttach is called multiple times for my ClientSelectionFragment which is a child of ModalDialogFragment. From my code, there are no instances that the onAttach is explicitly mentioned.

I tried to remove the @AndroidEntryPoint as well but still the same logs.

The center point of this crash log is the retainState mecahnism for the fragments. We all know about the method setRetainInstance(true) to retain state of our screens. My parent fragment is using this method and the value that was set as true.

Android SDK

The fix was to set the value as false to the retainInstance attribute. So i am guessing, because of retainInstance, somehow the hilt is attaching my fragment one more time with different context.

This fix my crash in only Samsung devices.

About sujan

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.