3

In recent google changes for android, version 1.3.0 of Fragment has also added a new FragmentOnAttachListener interface to replace the less-flexible onAttachFragment method. As in document mentioned:

This method is deprecated. The responsibility for listening for fragments being attached has been moved to FragmentManager. You can add a listener to this Activity's FragmentManager by calling FragmentManager.addFragmentOnAttachListener(FragmentOnAttachListener) in your constructor to get callbacks when a fragment is attached directly to the activity's FragmentManager.

So I implement this listener in onCreate() method of BaseActivity class that all my activities extends from it:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        _binding = getViewBinding()
        setContentView(_binding!!.root)

        val fm = supportFragmentManager

        val listener = FragmentOnAttachListener {
                fragmentManager, fragment ->
            // Respond to the fragment being attached.
            
        }

        fm.addFragmentOnAttachListener(listener)

    }

Source document explains:

Listener for receiving a callback immediately following Fragment.onAttach(Context). This can be used to perform any additional setup / provide any dependencies that the Fragment may need prior to child fragments being attached or the Fragment going through Fragment.onCreate(Bundle). Called after the fragment has been attached to its host. This is called immediately after Fragment.onAttach(Context) and before Fragment.onAttach(Context) has been called on any child fragments.

In MainActivity I have ButtomNavigationView setup with navigation component. The problem is when application runs for first time that fragments onAttach() called, this listener receive callback immediately before onAttach(), but in next time when onAttach() called manually, listener doesn't receive callback. To be clear, I have a button in one of my fragment that in its click event, fragment be refreshed and so onAttach() called again. When this happen, FragmentOnAttachListener doesn't work. Can anyone help me why listener doesn't receive callback again? Thanks in advance.

Reyhane Farshbaf
  • 453
  • 6
  • 12

2 Answers2

0

The same behavior, use again Activity.onAttachFragment or Fragment.onAttachFragment

I think, it is a bug. We need post an issue

0

I've run into a similar problem when a configuration change does not trigger my FragmentOnAttachListener, but a fresh start of the Activity was fine. In my case, it was because I called super.onCreate(savedInstanceState) first, before registering my listener with the FragmentManager. This call to super internally recreates and attaches any old attached Fragments, so my listener never got called the 2nd time around. I was able to get things working again by deferring the call to super to just after my getSupportFragmentManager().addFragmentOnAttachListener().

For reference, here was the stack trace from super.onCreate all the way to the dispatch that calls any listeners when it restores old state, but at that point I hadn't added my own listener yet:

dispatchOnAttachFragment:3266, FragmentManager
performAttach:3068, Fragment
attach:464, FragmentStateManager
moveToExpectedState:254, FragmentStateManager
moveToExpectedState:113, FragmentStore
moveToState:1424, FragmentManager
dispatchStateChange:2968, FragmentManager
dispatchCreate:2875, FragmentManager
dispatchCreate:252, FragmentController
onCreate:220, FragmentActivity
onCreate:77, MyActivity  // calling super.onCreate(savedInstanceState);

How exactly are you refreshing your fragment to trigger another onAttach?

qix
  • 7,228
  • 1
  • 55
  • 65