I have an application in which an activity launches various fragments. These fragments need to communicate with the parent activity, send some data in this case in the form of a HashMap.
In order for the fragment to send the data back to the parent activity, I have attempted to implement an interface, as recommended here and used this tutorial for further info. Here is my code for my fragment, and for my activity.
Activity
public class MyActivity extends AppCompatActivity {
@Override // Error comes up here
public void sendDataToActivity(HashMap<Integer, Boolean> myHashmap){
// do whatever I need here
}
}
Fragment
public class MyFragment extends Fragment {
private MyInterface dataPasser;
public interface MyInterface {
public void sendDataToActivity(HashMap<Integer, Boolean> myHashmap);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof MyInterface){
dataPasser = (MyInterface) context;
}
}
}
The error I'm getting is "method does not override or implement a method from a supertype". As far as I know this means something in my implementation of the interface in my activity is wrong and doesn't quite make it link with the interface in the fragment; but I've been trying to sort this out for a while, have looked at various answers here and haven't been able to find a way of getting it to work.
Extra information that can be useful is that MyActivity contains a TabLayout which is populated with various instances of MyFragment, I don't know if that might have something to do.
Feel free to ask for further information, code clarifications or point me to an answer that will help.