How to implement OnFragmentInteractionListener

后端 未结 12 2643
花落未央
花落未央 2020-11-22 06:25

I have a wizard generated app with navigation drawer in android studio 0.8.2

I have created a fragment and added it with newInstance() and I get this error:

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 07:18

    Just an addendum:

    OnFragmentInteractionListener handle communication between Activity and Fragment using an interface (OnFragmentInteractionListener) and is created by default by Android Studio, but if you dont need to communicate with your activity, you can just get ride of it.

    The goal is that you can attach your fragment to multiple activities and still reuse the same communication approach (Every activity could have its own OnFragmentInteractionListener for each fragment).

    But and if im sure my fragment will be attached to only one type of activity and i want to communicate with that activity?

    Then, if you dont want to use OnFragmentInteractionListener because of its verbosity, you can access your activity methods using:

    ((MyActivityClass) getActivity()).someMethod()
    

提交回复
热议问题