I have an activity with several fragments containing web views.
I have exposed the web views canGoBack
and goBack
to the MainActivity
of the application.
At the moment I have it working for one fragment, but I am not sure how to check which fragment is sending the information, and in turn how to send the appropriate fragment the response.
Here is the code involved from the MainActivity:
@Override
public void onBackPressed() {
circleFragment.canGoBack();
if (circleFragment.canGoBack()) {
circleFragment.goBack();
} else {
super.onBackPressed();
}
}
Instead of implementing a boolean canGoBack()
Just create an interface "Backable"
public interface Backable {
public void goBack();
public boolean canGoBack();
}
Make your Fragment implement Backable
and use if(myFragment instanceof Backable)
to now if your current Fragment is backable
So:
@Override
public void onBackPressed() {
if(myFragment instanceof Backable) {
((Backable) myFragment).canGoBack();
if(((Backable) myFragment).canGoBack()){
((Backable) myFragment).goBack();
}else{
super.onBackPressed();
}
}else{
super.onBackPressed();
}
}