简体   繁体   中英

How can I reload a URL inside a Webview Fragment on screen rotate?

i have the following webView inside a Fragment. My big Problem is, that the specified URL will load on a screen rotate but not the actual URL, which was achieved by clicks. The "onConfigurationChanged" Method will not work.

Is there a way to reload the actually URL on screen rotate ?

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;
}
}

Much thanks for help !

You can save the current url of your webview in onSaveInstanceState() and load it on recreation:

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);
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM