I am trying to use the built in back button such that it goes back 1 screen within a webview or within the app( no matter where) by 1, so suppose if i have a news section and I click on it, it will display a news page. Then I click on search and go to a webview and when I click on back it should show the news section page i last opened. Also, if I am in the webview and navigate within the webview and click back then it should show me a page within the webview. I have added some back code which works within a webview. However, it does not work for the enws section page. It takes me back to the news section instead of the specific page I opened last under news section. How do I fix my code to achieve the same? Here's the code I added for back:
private boolean goingBack = false;
private boolean onBackPressClearStack = true;
public void setOnBackPressClearStack(boolean b){
onBackPressClearStack = b;
}
public boolean webViewSteppedBack() {
if (mWebview != null && mWebview.canGoBack()) {
mWebview.goBack();
return true;
}
return false;
}
@Override
public boolean backPressed(final MainActivity mainActivity) {
if (webViewSteppedBack()) {
return true;
}
if (onBackPressClearStack) {
goingBack = true;
FragmentUtils.onBackPressedKnockFragsOffStack(mainActivity, this);
}
return false;
}
public static MyWebViewFragment newInstanceNoBackPressed(final FragmentManager manager, final String searchTerm, final String symbolType, int containerViewId) {
MyWebViewFragment fragment = __newInstance(new MyWebViewFragment(), manager, searchTerm, symbolType, containerViewId);
fragment.setOnBackPressClearStack(false);
return fragment;
}
public static MyWebViewFragment newInstanceNoBackPressed(final MyWebViewFragment fragment, final FragmentManager manager, final String searchTerm, final String symbolType, int containerViewId) {
fragment.setOnBackPressClearStack(false);
return __newInstance(fragment, manager, searchTerm, symbolType, containerViewId);
}
@Override
public void onResume() {
super.onResume();
final MainActivity activity = (MainActivity) getActivity();
activity.updateActionBarTitle();
activity.setBackPressListener(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (goingBack) {
return null;
}
final MainActivity activity = (MainActivity) getActivity();
activity.setBackPressListener(this);
View view = inflater.inflate(R.layout.fragment_search_answers, container, false);
my webview code.....
return view;
}