How to fix the rotation on the web browser for Android phones?

I am trying to figure out how to fix a webview orientation problem. Basically, every time the user changes the orientation on the device, the program is white and reloads the entire page. It lasts a long time.

I would like it to work like any other program that simply adjusts the page size to fit the orientation. I tried to read other articles on this subject, but when I implement their solution, it does not seem to fix the problem.

Here is my current code.

package testdev.HelloWebApp;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class HelloWebAppActivity extends Activity {
/** Called when the activity is first created. */
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    webView = (WebView)findViewById(R.id.webView);
    WebSettings webSettings =webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.setWebViewClient (new HelloWebViewClient());
    webView.loadUrl("http://google.com");

    if (savedInstanceState != null)
      ((WebView)findViewById(R.id.webView)).restoreState(savedInstanceState);

}
private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack())
    {
        webView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
protected void onSaveInstanceState(Bundle outState ){
    ((WebView) findViewById(R.id.webView)).saveState(outState);
}
}
+2
source share
4 answers

Android 3.2 ( API 13), " " , . , API 13 ( minSdkVersion targetSdkVersion), "screenSize" "". , decalare

<activity android:configChanges="orientation|screenSize">

docs: http://developer.android.com/guide/topics/resources/runtime-changes.html

+7

. , webView , ( , ), , webView onCreate(), webView .

AndroidManifest.xml:

<activity android:configChanges="orientation">

if-:

...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Add this
    if(webView == null){
        webView = (WebView)findViewById(R.id.webView);
        WebSettings webSettings =webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.setWebViewClient (new HelloWebViewClient());
        webView.loadUrl("http://google.com");
    }

    if (savedInstanceState != null)
        ((WebView)findViewById(R.id.webView)).restoreState(savedInstanceState);
}

...
+4

Android , . . , Android reset . ( WebView). .

, .

< android: name= ". MyActivity" android: configChanges = "orientation" >

- , .

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Do something here
}
+1

2015 , , Jellybean, KK Lollipop. - . , - . , , - , - . ... ( ):

<application
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name="com.myapp.abc.app">

    <activity
            android:name=".myRotatingActivity"
            android:configChanges="keyboard|keyboardHidden|orientation">
    </activity>

:

     public class app extends Application {
            public static WebView webview;
            public static FrameLayout webviewPlaceholder;//will hold the webview

         @Override
               public void onCreate() {
                   super.onCreate();
    //dont forget to put this on the manifest in order for this onCreate method to fire when the app starts: android:name="com.myapp.abc.app"
                   setFirstLaunch("true");
           }

       public static String isFirstLaunch(Context appContext, String s) {
           try {
          SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
          return prefs.getString("booting", "false");
          }catch (Exception e) {
             return "false";
          }
        }

    public static void setFirstLaunch(Context aContext,String s) {
       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(aContext);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("booting", s);
            editor.commit();
           }
        }

ACTIVITY put:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(app.isFirstLaunch.equals("true"))) {
            app.setFirstLaunch("false");
            app.webview = new WebView(thisActivity);
            initWebUI("www.mypage.url");
        }
}
@Override
    public  void onRestoreInstanceState(Bundle savedInstanceState) {
        restoreWebview();
    }

public void restoreWebview(){
        app.webviewPlaceholder = (FrameLayout)thisActivity.findViewById(R.id.webviewplaceholder);
        if(app.webviewPlaceholder.getParent()!=null&&((ViewGroup)app.webview.getParent())!=null) {
            ((ViewGroup) app.webview.getParent()).removeView(app.webview);
        }
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
        app.webview.setLayoutParams(params);
        app.webviewPlaceholder.addView(app.webview);
        app.needToRestoreWebview=false;
    }

protected static void initWebUI(String url){
        if(app.webviewPlaceholder==null);
          app.webviewPlaceholder = (FrameLayout)thisActivity.findViewById(R.id.webviewplaceholder);
        app.webview.getSettings().setJavaScriptEnabled(true);       app.webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        app.webview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        app.webview.getSettings().setSupportZoom(false);
        app.webview.getSettings().setBuiltInZoomControls(true);
        app.webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        app.webview.setScrollbarFadingEnabled(true);
        app.webview.getSettings().setLoadsImagesAutomatically(true);
        app.webview.loadUrl(url);
        app.webview.setWebViewClient(new WebViewClient());
        if((app.webview.getParent()!=null)){//&&(app.getBooting(thisActivity).equals("true"))) {
            ((ViewGroup) app.webview.getParent()).removeView(app.webview);
        }
        app.webviewPlaceholder.addView(app.webview);
    }

, XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".myRotatingActivity">
    <FrameLayout
        android:id="@+id/webviewplaceholder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

Several things can be improved in my solution, but I have already spent a lot of time, for example: a shorter way to check if the action was launched the first time, instead of using the SharedPreferences repository. This approach saves your webview intact (afaik), its text fields, labels, user interface, javascript variables and navigation states that are not reflected in the URL.

+1
source

All Articles