0

I need to add a fragment to a view dynamically as it's also inside a fragment (see the answer https://stackoverflow.com/a/6847770/811405).

The whole structure is like this:

Fragment
|
|---ViewHolder
    |
    |---(dynamically added fragment)

In my viewholder, I have access to the view. I've first thought that I could call getContext() and then cast it to activity, then use it's fragment manager to add it. However, getContext returns my Application object, not the activity (it's called on app launch before app is physically on the screen, so maybe the activity hasn't been created yet).

How can I add fragment dynamically on ViewHolder? (of course, without using terrible anti-patterns)

Community
  • 1
  • 1
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389

1 Answers1

0

My real problem was inflating the layout from an incorrect context (yup, you guessed right, Application instance) instead of my Activity. I've fixed the code to make it inflate from the Activity, and now getContext returns the activity. I could then get the fragment manager and add the fragment normally.

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389