How to implement OnFragmentInteractionListener
Answers posted here did not help, but the following link did:
http://developer.android.com/training/basics/fragments/communicating.html
Define an Interface
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
...
}
For example, the following method in the fragment is called when the user clicks on a list item. The fragment uses the callback interface to deliver the event to the parent activity.
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
Implement the Interface
For example, the following activity implements the interface from the above example.
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
Update for API 23: 8/31/2015
Overrided method onAttach(Activity activity)
is now deprecated in android.app.Fragment
, code should be upgraded to onAttach(Context context)
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onStart() {
super.onStart();
try {
mListener = (OnFragmentInteractionListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().toString()
+ " must implement OnFragmentInteractionListener");
}
}
must implement OnFragmentInteractionListener error in Android Studio
in this case you should implement your listener in your activity not in a fragment.
public class LauncherActivity extends AppCompatActivity implements Tab1.OnFragmentInteractionListener
This is because Context passed in your fragment comes from activity and not fragment.
If you want HomeFragment to receive events. What you can do is create public setter method for listeners in Tab-fragments.
public void setInteractionListener(OnFragmentInteractionListener mListener){
this.mListener = mListener;
}
Must implement OnFragmentInteractionListener()
You need to make the parent activity implement OnFragmentInteractionListener
and override the methods provided by that interface.
class MyActivity implements OnFragmentInteractionListener
This is where your code is crashing, your activity probably does not implement the metioned interface and because of that the crash happens when the fragment is attached:
@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");
}
}
ERROR: must implement OnFragmentInteractionListener
The OnFragmentInteractionListener
interfaces in both of your Fragment
s contain a method with the same signature, and MainActivity
is implementing one method to try to cover both interfaces.
Change the signature of one or both of the methods, then implement both methods in MainActivity
. For example:
NavFragment
public interface OnFragmentInteractionListener {
public void onNavFragmentInteraction(String string);
}
ContentFragment
public interface OnFragmentInteractionListener {
public void onContentFragmentInteraction(String string);
}
MainActivity
@Override
public void onNavFragmentInteraction(String string) {
// Do stuff
}
@Override
public void onContentFragmentInteraction(String string) {
// Do different stuff
}
How to add OnFragmentInteractionListener in Navigation Drawer Activity in Kotlin?
Apparently, You are trying to implement communication mechanism between fragment and activity via OnFragmentInteractionListener
interface so use
class MainActivity : AppCompatActivity(),
FirstFragment.OnFragmentInteractionListener,
NavigationView.OnNavigationItemSelectedListener {
// .. other code
override fun onFragmentInteraction(uri: Uri) {
// TODO Implement
}
}
and override methods
MainActivity must implement OnFragmentInteractionListener
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, OnFragmentInteractionListener {
.......
}
Press alt +enter on OnFragmentInteractionListener
to import it in mainActivity
Android Studio getting Must implement OnFragmentInteractionListener
Remove onAttach
method from your Fragment, then it'll be okay.
BottomNavigationView in a fragment, getting a 'must implement OnFragmentInteractionListener' error
Sounds like you copy/pasted some code / left in part of some auto-generated sample code. If you're not using the OnFragmentInteractionListener
interface, just remove that code.
Related Topics
Detect Network Connection Type on Android
How to Adjust Text Font Size to Fit Textview
Android 11 Scoped Storage Permissions
Integrating the Zxing Library Directly into My Android Application
Android: Difference Between Parcelable and Serializable
How to Completely Uninstall Android Studio from Windows(V10)
Accepting Https Connections With Self-Signed Certificates
How to Store Image as Blob in Sqlite & How to Retrieve It
How to Send a "Multipart/Form-Data" Post in Android With Volley
Getting Exception "Illegalstateexception: Can Not Perform This Action After Onsaveinstancestate"
Android Fragment Handle Back Button Press
Onsaveinstancestate () and Onrestoreinstancestate ()
How to Make a Custom Keyboard in Android
When to Call Activity Context or Application Context