Search code examples
androidlistkotlinandroid-fragmentssuperclass

Only one class may appear in a supertype list


I am running into trouble with having two superclasses. When I am adding the MainActivity() I am recieving the error: Only one class may appear in a supertype list.

Any ideas on how I can work around this problem?

class ExaminationFragment : Fragment(),MainActivity()  {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_examination, container, false)
    }

}

Solution

  • In Kotlin you can inherit only one class, but multiple interfaces. In your case Fragment and MainActivity are classes, you can't inherit both of them. I guess you don't need to inherit MainActivity class by fragment class ExaminationFragment, inheriting Fragment class is enough for displaying a screen:

    class ExaminationFragment : Fragment() { ... }
    

    Please see how to create Fragments. In the provided example ExampleFragment is inherited from Fragment class, and doesn't inherit from any Activity class.