Reputation: 1020
i have a created a fragment and can't find what is the use of Interface class in this Fragment...i google it but can't find the right documentation?
Thank you for your concern!
public class SongListFragment extends Fragment {
public SongListFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
songIds = getArguments().getIntArray(SONG_IDS);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
//what's the use?
public interface OnFragmentInteractionListener {
public void onSongSelected(int songId);
}
}
Upvotes: 1
Views: 1732
Reputation: 758
An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.
You might have a brief idea over here Feel free to ask if any confusion rises! :)
So in your particular case your Activity must implement that interface
OnFragmentInteractionListener
otherwise the fragments which are attached in the Activity
cannot communicate with each other. Your activity should look like
public class YourActivity extends Activity implements OnFragmentInteractionListener
Then in your Activity
you implement the method onSongSelected(int songId)
You might get help from here. Hope this helps!
Upvotes: 2
Reputation: 23881
Interface is used to communicate between fragment
and activity
or between multiple fragments
when an event occurs.
See the Documentation
in your case:
public static class MainActivity extends Activity
implements SongListFragment.OnFragmentInteractionListener{
...
public void onSongSelected(int songId) { //this method must be implemented
// The user selected the song from the list in SongListFragment
// Do something here to display that song..in your activity
}
}
you can implement the interface in your activity and write the onSongSelected
method with the song id passed as parameter.
So basically it is used to pass selected song list information to other activity
or fragment
when a selection occurs
Upvotes: 0
Reputation: 13153
OnFragmentInteractionListener
could be use to communicate between fragments
To allow a
Fragment
to communicate up to its Activity, you can define an interface in theFragment
class and implement it within the Activity. The Fragment captures the interface implementation during itsonAttach()
lifecycle method and can then call the Interface methods in order to communicate with the Activity.
Find another SO example here
Upvotes: 2
Reputation: 4249
@Tahmid Rahman explains what an interface is in there answer.
In this specific case the interface should be implemented in the Activity that your fragment is attaching to. That will allow the fragment to call onSongSelected()
on the activity. Then the activity can in turn properly handle the users requested action.
Without the interface, there would not be a well defined way for the fragment to tell its parent activity that the user had clicked on a song.
Upvotes: 0