Search code examples
androidxandroid-tvleanback

Replacement of deprecated BrowseFragment AndroidTV


This class was deprecated in API level 27.1.0. use BrowseSupportFragment

But when i replaced this

public class MainFragment extends BrowseFragment 

to

public class MainFragment extends BrowseSupportFragment

bellow exception is occurring

Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class MainFragment that is not a Fragment

My XML code is

<?xml version="1.0" encoding="utf-8"?>
  <fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:name="com.example.tvapplocation1.MainFragment"
  android:layout_width="match_parent" android:layout_height="match_parent">
</fragment>

Solution

  • Short answer: This can happen if your Activity does not extend from FragmentActivity.

    Longer answer: There are two versions of the Fragment class, one that's built into the OS and one that's part of the support/Android X libraries (). You should always use the support/Android X version because it provides compatibility and consistent behavior across Android OS versions. The various *SupportFragment classes (like BrowseSupportFragment) extend from the support/Android X version of Fragment, which require you to use the FragmentActivity from the support/Android X libraries.