How to implement OnFragmentInteractionListener

后端 未结 12 2504
花落未央
花落未央 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:29

    OnFragmentInteractionListener is the default implementation for handling fragment to activity communication. This can be implemented based on your needs. Suppose if you need a function in your activity to be executed during a particular action within your fragment, you may make use of this callback method. If you don't need to have this interaction between your hosting activity and fragment, you may remove this implementation.

    In short you should implement the listener in your fragment hosting activity if you need the fragment-activity interaction like this

    public class MainActivity extends Activity implements 
    YourFragment.OnFragmentInteractionListener {..}
    

    and your fragment should have it defined like this

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
    

    also provide definition for void onFragmentInteraction(Uri uri); in your activity

    or else just remove the listener initialisation from your fragment's onAttach if you dont have any fragment-activity interaction

提交回复
热议问题