[英]How do I go back within webview after navigation to a different fragment from the webview fragment?
I have been trying to figure out a way to go back within WebView
after leaving the WebView fragment
. 在离开
WebView fragment
后,我一直想找到一种在WebView
返回的方法。 The dilemma is as follows: 困境如下:
WebView fragment
page1.html
WebView fragment
page1.html
page1.html?navigate=page2.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"
Fragment
上的某些事件的点击WebView fragment
,说我点击,在带来了一个符号"NotAwebviewfragment.java"
notawebviewfragment
then it takes me to the WebView page
I left from. notawebviewfragment
单击回来然后它转到我离开的WebView page
。 However when I click back again it take me to the previous Fragment
that was open before WebView
. WebView
之前打开的Fragment
。 WebViewFragment
I can navigate back and forth as desired. WebViewFragment
我可以根据需要来回导航。 However, once I leave it, it gives me access to the WebView
only once. WebView
一次。 Here's the code so far: Within the webview: 这是迄今为止的代码:在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): MainActivity
(包含后面的片段导航代码):
@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的片段中覆盖此方法。
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.
在activity的onBackPressed方法中,您可以获取当前活动的片段。 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.
您可以检查活动片段是否是Awebviewfragment的实例,如果webview可以导航回来,否则使用片段管理器的popBackStack ()方法从堆栈中弹出当前活动片段。 Here are the code snippet: 1. to add fragment
以下是代码段:1。添加片段
private void addNotAwebviewfragment() { supportFragmentManager.beginTransaction(); private void addNotAwebviewfragment(){supportFragmentManager.beginTransaction(); FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); notAwebviewfragment = NotAwebviewfragment.newInstance("test", "test");
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() { 在主活动中的onBackPRessed()中:@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 . 你可以检查github中提交的样本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.