I have one activity in my app and I have 2 fragments - A and B. In MainActivity layout there is a FrameLayout. From MainActivity I call FragmentManager to load fragment A into FrameLayout. Then, from fragment A I load fragment B into FrameLayout. In my fragment B I implement onBackPressed() and *onKeyDown(int _a, KeyEvent _b)* from MainActivity to make back button work with WebView in fragment B. But it works as if I didn't implement it!
MainActivity interface:
public interface onKeyDownListener {
public void onBackPressed();
public boolean onKeyDown(int _a, KeyEvent _b);
}
Fragment B implementation:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return getActivity().onKeyDown(keyCode, event);
}
public void onBackPressed() {
if (mWebView.isFocused() && mWebView.canGoBack()) {
mWebView.goBack();
} else {
Toast.makeText(getActivity().getApplicationContext(), "This's working!", 50000).show();
getActivity().onBackPressed();
}
}
No one of these methods works, back button just close the application.