Skip to content Skip to sidebar Skip to footer

Interface Communication From Fragment To Activity Issue

I've recently tried to use an interface for fragment-activity communication. The idea is that when a button is pressed in a fragment, it retrieves data from an EditText in the same

Solution 1:

getActivity() but it cannot find the associated method within the fragment

This is because getActivity() returns an Activity, not a MainActivity which is your custom subclass. You can easily fix this with a cast. For example, in your fragment, you can do this:

OnDataPassmain= (OnDataPass) getActivity();
main.onDataPass(message);

Since such a cast is required, the interface seems to get in the way in my opinion. You can just as easily cast directly to MainActivity:

MainActivitymain= (MainActivity) getActivity();
main.onDataPass(message);

Post a Comment for "Interface Communication From Fragment To Activity Issue"