Search code examples
androidandroidxandroid-jetifier

requireActivity(), requireContext() and requireFragmentManager() wont work with AndroidX


I've been working on updating out project which contains DataBinding, to the new AndroidX packages. There were a number of issues with misuse of the DataBinding SafeUnbox methods, but these are resolved and I am currently prevented from building because AndroidX does not seem to support the requireActivity, requireContext and requireFragmentManager methods.

I have android.enableJetifier=true and android.useAndroidX=true in my gradle properties and have fixed minor issues it presented, but changing from android.support.v4.app.Fragment has not been required (thankfully - because libraries like trello.rxlifecycle then break)

So even though the Fragment and Activity classes/imports are unchanged, I can no longer use the require methods. Is there a fix for this?


Solution

  • I was experiencing this first with Android Studio 3.1.2. No other build issues were presented. After I did an upgrade to Android Studio to 3.2 and used the Migrate tool (Refactor -> Migrate to AndroidX) I saw other, previously hidden build issues. Once these other (hidden) issues were resolved I was able to build the project despite the IDE showing errors with these methods..

    Note: even though I can build normally, the calls to requireActivity still show red in Android Studio, so if you see this I encourage you to try to build anyway