在我的navigationDrawer
菜单中,点击搜索会在webview
中打开特定的fragment
。工作正常,但当我按下webview
时 - 而不是上次访问webscreen
,它会回到我的主fragment
。我怎么能解决这个问题?我在这里提供我的fragment
java文件。请提及任何其他需要的代码(如果需要)来解决我的问题
public class GoogleFragment extends Fragment {
public WebView mWebView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(fragment_google, container, false);
mWebView = (WebView) v.findViewById(webview);
mWebView.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//improve webview performance
mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return v;
}
}
答案 0 :(得分:0)
我认为当您按下片段中的后退按钮时,我们需要在Activity中处理它,
您需要在主要活动中首先覆盖 onBackPressed()方法,其中包含片段和全局分配您的Google片段中包含 Webview
注意:当您添加或替换片段时,请继续添加 TAG ,这对获取片段实例很有帮助。
@Override
public void onBackPressed() {
Fragment fragment =
getFragmentManager().findFragmentByTag("google_fragment");
if (fragment instanceof GoogleFragment) {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}else{
super.onBackPressed();
}
}
答案 1 :(得分:0)
public class GoogleFragment extends Fragment {
public WebView mWebView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(fragment_google, container, false);
mWebView = (WebView) v.findViewById(webview);
mWebView.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//improve webview performance
mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
v.setFocusableInTouchMode(true);
v.requestFocus();
v.setOnKeyListener( new View.OnKeyListener()
{
@Override
public boolean onKey( View v1, int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK ) {
if(mWebView.canGoBack())
{
wView.goBack();
wView.getUrl();
}
return true;
}
return false;
}
} );
return v1;
}
}
Please try this code .it may help you.