【发布时间】:2013-04-22 18:41:56
【问题描述】:
这是基本Fragment 的代码,其中包含WebView。WebFragment.java
public class WebFragment extends Fragment {
String TAG = "WebFragment";
private WebView webView;
private String currentUrl = "http://www.google.com";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.webview_layout, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
webView = (WebView) getView().findViewById(R.id.helloWebview);
initWebView();
webView.loadUrl(currentUrl);
}
@SuppressLint("SetJavaScriptEnabled")
private void initWebView() {
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JavaScriptInterface(), "HTMLOUT");
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.e(TAG, "Loding...");
super.onPageStarted(view, url, favicon);
}
});
}
class JavaScriptInterface {
public void showHTML(String html) {
}
}
}
我还有两个基本片段(没有WebView)。然后我使用FragmentAdapter将所有这3个Fragments放入ViewPager(WebFragment、Fragment1和Fragment2分别)。我在AndroidManifest.xml 中添加了android:configChanges="orientation|screenSize"(所以WebView 不会在配置更改时重新生效)。
现在我的问题是,在运行应用程序时,我将ViewPager 的第一页(WebFragmet)更改为第二页(Fragment1),然后我又回到了第一页。所以没问题WebFragment 就是这样。
现在我再次将ViewPager 更改为WebFragment-->Fragment1-->Fragment2 并通过反转上述操作的方向返回到第一页。但当时我看到WebFragment中的WebView正在重新加载。
还有一个问题,每当WebView 开始加载时,我都会看到 2 个类似的日志条目。
WebFragment Loading...
WebFragment Loading...
所以我的问题是:
1) 当WebFragment 从BackStack 保留时,如何防止WebView 重新加载?
2) 为什么在这里WebView 加载两次?
只要看到这个问题并帮助我(Prevent WebView reloads in FragmentPagerAdapter?)。
【问题讨论】:
标签: android android-fragments android-webview android-viewpager back-stack