How to fix rotation on webview for Android phones?

后端 未结 4 1240
野性不改
野性不改 2021-01-15 04:48

I\'m trying to figure out how to fix the orientation problem on webview. Basically every time the user changes the orientation on the device the program goes white and reloa

4条回答
  •  自闭症患者
    2021-01-15 05:38

    It is 2015, and many people are looking for a solution that still workds on Jellybean, KK and Lollipop phones. After much struggling I found a way to preserve the webview intact after you change orientation. My strategy is basically to store the webview in a separate static variable in another class. Then, if rotation occurs, I dettach the webview from the activity, wait for the orientation to finish, and reattach the webview back to the activity. For example... first put this on your MANIFEST (keyboardHidden and keyboard are optional):

    
    
        
        
    

    In a SEPARATE APPLICATION CLASS, put:

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

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

    Finally, the simple XML:

    
        
    
    

    There are several things that could be improved in my solution, but I already spent to much time, for example: a shorter way to validate if the Activity has been launched for the very first time instead of using SharedPreferences storage. This approach preserves you webview intact (afaik),its textboxes, labels, UI, javascript variables, and navigation states that are not reflected by the url.

提交回复
热议问题