I have an activity with a fragment. I'm trying to execute some code when the user presses the back button. I'm trying to do so with this method in the activitiy's class:
@Override
public void onBackPressed() {
super.onBackPressed();
}
and then I override it in my fragment with this code:
@Override
public void onBackPressed() {
((MyActivity)getActivity()).onBackPressed();
doStuff();
}
What I'm trying to do here is to have the onBackPressed method overriden in the fragment's class and then have that method first call the method in its activity as I can't call getActivity().super.onBackPressed() from the fragment's onBackPressed() method. I've tried this and it doesn't work. In the activity's class it overrides the method just fine, but in the fragment's class the compiler complains that the fragment's onBackPressed() method doesn't override the method in it's super class. So, my question is, how can I do so correctly? Is that even possible?
Another way I could solve my problem is to have the full onBackPressed() method in my activity and have that one call a method of my fragment, but I can't do that as my fragment doesn't have an id (it is generated by the FragmentPagerAdapter).