I have been trying to figure out a way to go back within WebView
after leaving the WebView fragment
. The dilemma is as follows:
WebView fragment
page1.html
page1.html?navigate=page2.html
. Fragment
on click of some event in the WebView fragment
, say I click a symbol that brings in "NotAwebviewfragment.java"
notawebviewfragment
then it takes me to the WebView page
I left from. However when I click back again it take me to the previous Fragment
that was open before WebView
. WebViewFragment
I can navigate back and forth as desired. However, once I leave it, it gives me access to the WebView
only once. Here's the code so far: Within the webview:
public boolean webViewSteppedBack() {
if (webview != null && webview.canGoBack()) {
webview.goBack();
return true;
}
return false;
}
public boolean backPressed(final MainActivity mainActivity) {
if (webViewSteppedBack()) {
if(!EikonUtils.isTablet(getActivity())) {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
return true;
}
return false;
}
MainActivity
(which contains the fragment navigation code for back):
@Override
public void onBackPressed() {
final FragmentManager manager = getSupportFragmentManager();
Fragment topFragment = FragmentStackManager.getInstance().getTopFragment();
if (backPressListener != null) {
boolean b = false;
//Making sure we trigger the backPressed event if the listener is the top fragment
String bplTag = ((Fragment) backPressListener).getTag();
String topFragemtnTag = "";
if (topFragment != null) {
topFragemtnTag = topFragment.getTag();
if (bplTag != null && topFragemtnTag != null && bplTag.equals(topFragemtnTag)) {
b = backPressListener.backPressed(this);
}
}
if (b) { return; }
if (!NotAWebViewFragment.TAG_NOT.equals(bplTag)) {
backPressListener = null;
}
}
}
You need to override this method in your fragment in which you are defining the webView.
webView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
}
}
}
return false;
}
});
You can add all the fragment to the back stack . In activity's onBackPressed method, you can get the current active fragment. You can check if the active fragment is the instance of Awebviewfragment and if the webview can navigate back otherwise pop the current active fragment from the stack using popBackStack () method of fragment manager. Here are the code snippet: 1. to add fragment
private void addNotAwebviewfragment() { supportFragmentManager.beginTransaction(); FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); notAwebviewfragment = NotAwebviewfragment.newInstance("test", "test");
fragmentTransaction.add(R.id.contents, notAwebviewfragment, NotAwebviewfragment.TAG_NOT).addToBackStack(NotAwebviewfragment.TAG_NOT);
fragmentTransaction.commit();
}
private void addFragWebview(){
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
webViewFragment = Awebviewfragment.newInstance("test", "test");
webViewFragment.setFragmentListener(weblistener);
backPressListener = webViewFragment;
fragmentTransaction.add(R.id.contents, webViewFragment, Awebviewfragment.TAG).addToBackStack(Awebviewfragment.TAG);
fragmentTransaction.commit();
}
In onBackPRessed() in main activity: @Override public void onBackPressed() {
Fragment fragment = getActiveFragment(); if(fragment instanceof Awebviewfragment){ System.out.println("web view fragment"); if(backPressListener != null && backPressListener.backPressed(this)){ System.out.println("web view fragment will return"); return; } } int count = supportFragmentManager.getBackStackEntryCount(); if (count > 0 ){ supportFragmentManager.popBackStackImmediate(); }else { super.onBackPressed(); }
}
you can check with the sample committed in the github .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.