我在片段中有以下 webView 。 我的大问题是,指定的URL将在屏幕上加载旋转而不是实际的URL,这是通过点击实现的。 “onConfigurationChanged”方法不起作用。
有没有办法在屏幕旋转时重新加载实际的URL?
public class IndikatorenFragment extends Fragment {
public WebView webView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.content_fragment, container, false);
// getActivity() will hand over the context to the method
// if you call this inside an activity, simply replace getActivity() by "this"
if(!isConnected(getActivity())) buildDialog(getActivity()).show();
webView = (WebView) v.findViewById(R.id.webPage);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/indikatoren.html");
webView.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
viewx.loadUrl(urlx);
return false;
}
});
webView.getSettings().setGeolocationEnabled(true);
webView.setOnKeyListener(new View.OnKeyListener() {
// allows going back in the web view by pressing return button
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView webView = (WebView) v;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
return true;
}
break;
}
}
return false;
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
// Allow Geolocation
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
return v;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
public boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if (netinfo != null && netinfo.isConnectedOrConnecting()) {
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting()))
return true;
else return false;
} else return false;
}
public AlertDialog.Builder buildDialog(Context c) {
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setTitle("Es besteht keine Internet Verbindung");
builder.setMessage("Bitte stellen sie eine Verbindung zum Internet her, da die Rasterkarten eine intakte Verbindung benötigen.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder;
}
}
非常感谢您的帮助!
答案 0 :(得分:1)
您可以将您的网络视图的当前网址保存在onSaveInstanceState()
中,并将其加载到娱乐中:
public class IndikatorenFragment extends Fragment {
public WebView webView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
webView.loadUrl(savedInstanceState != null ? savedInstanceState.getString("url") : "file:///android_asset/indikatoren.html");
...
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("url", webView.getUrl());
super.onSaveInstanceState(outState);
}
}