代码之家  ›  专栏  ›  技术社区  ›  Paul Spiesberger

带有SherlockActionBar的WebView在旋转时重新加载

  •  0
  • Paul Spiesberger  · 技术社区  · 13 年前

    我知道有很多类似的问题,相信我,我试了很多,但都没有成功。我有一个使用Webview的Android应用程序。你可以在Play Store中找到我的应用程序:

    https://play.google.com/store/apps/details?id=at.rk.rps&feature=search_result#?t=W251bGwsMSwxLDEsImF0LnJrLnJwcyJd

    在Play Store呆了几个月后,我决定进行更新,并在我的新设计中使用Android设计指南,所以Actionbar是必要的。我选择了ActionbarSherlock,一切都很好,只是每次我转动手机时,Webview都会重新加载。在我的上一个版本中,我用这个答案处理了这个问题,它起到了作用:

    My Webview Reloads When I Change From Portrait To Landscape

    但现在它不会再工作了。我也尝试过这种解决方案:(我还在网站末尾留下了评论)

    http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/

    同样令人失望的结果。我甚至不确定ActionbarSherlock是否是问题所在,因为我已经改变了很多,但不可能重现旧的状态!

    这就是应用程序的样子(这只是在新版本中实现的,你在Play Store上发布的版本中找不到它):

    loading screen

    PayPal site

    以下是我代码的一部分,首先是清单,然后是布局,然后是java代码:

    清单:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="at.rk.rps"
        android:versionCode="8"
        android:versionName="0.4" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="15" />
    
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.CALL_PHONE" />
    
        <application
            android:icon="@drawable/rps"
            android:label="@string/app_name"
            android:theme="@style/Theme.Sherlock.Light.DarkActionBar" >
            <activity
                android:name=".RPSActivity"
                android:configChanges="orientation"
                android:label="@string/app_name" >
            </activity>
            <activity
                android:name=".SettingsActivity"
                android:label="@string/app_name"
                android:windowSoftInputMode="stateHidden" >
            </activity>
            <activity
                android:name=".DonateActivity"
                android:label="@string/app_name" >
            </activity>
            <activity
                android:name=".PayPalActivity"
                android:configChanges="orientation"
                android:label="@string/app_name" >
            </activity>
            <activity
                android:name=".LoadingScreen"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/paypal_loadingtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="90dp"
            android:gravity="center"
            android:text="@string/paypal_loadingtext"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/rk_red" />
    
        <ProgressBar
            android:id="@+id/paypal_progressbar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/paypal_loadingtext"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:layout_marginTop="10dp" />
    
             <WebView
            android:id="@+id/paypal_webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:visibility="gone" />
    
    </RelativeLayout>
    

    Java代码:

    package at.rk.rps;
    
    import org.apache.http.util.EncodingUtils;
    
    import android.content.res.Configuration;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.WindowManager;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.LinearLayout;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    
    import com.actionbarsherlock.app.ActionBar;
    import com.actionbarsherlock.app.SherlockActivity;
    import com.actionbarsherlock.view.Menu;
    import com.actionbarsherlock.view.MenuInflater;
    import com.actionbarsherlock.view.MenuItem;
    
    public class PayPalActivity extends SherlockActivity {
    
    private ActionBar actionbar;
        private WebView webview;
        private TextView loadingtxt;
        private ProgressBar progressbar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            if(getSharedPreferences(RPSActivity.PREFS_NAME, 0).getBoolean(RPSActivity.FULLSCREEN, true)) this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
            setContentView(R.layout.paypal);
    
            initUI();
        }
    
        private void  initUI() {
    
            actionbar = getSupportActionBar();
            actionbar.setTitle(R.string.paypal_actionbar_headline);
            actionbar.setHomeButtonEnabled(true);
            this.actionbar.setDisplayHomeAsUpEnabled(true);
    
            loadingtxt = (TextView) findViewById(R.id.paypal_loadingtext);
            progressbar = (ProgressBar) findViewById(R.id.paypal_progressbar);
    
            webview = (WebView) findViewById(R.id.paypal_webview);
    
            webview.requestFocus(View.FOCUS_DOWN); // Enables the keyboard in landscape mode
            webview.setOnTouchListener(new View.OnTouchListener() {             
                @Override
                public boolean onTouch(View v, MotionEvent event) {
    
                    switch (event.getAction()) { 
                    case MotionEvent.ACTION_DOWN: 
                    case MotionEvent.ACTION_UP:
                        if (!v.hasFocus()) { 
                            v.requestFocus(); 
                        } 
                        break; 
                    }
    
                    return false; 
                }
            });
    
            webview.getSettings().setJavaScriptEnabled(true); // Enables JavaScript
            webview.getSettings().setBuiltInZoomControls(true); // Enables zoom
    
            webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); // Displays scrollbars outside the view
            webview.setScrollbarFadingEnabled(false);       
    
            webview.getSettings().setLoadWithOverviewMode(true);
            webview.getSettings().setUseWideViewPort(true);
    
            webview.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
    
                    loadingtxt.setVisibility(TextView.VISIBLE);
                    progressbar.setVisibility(ProgressBar.VISIBLE);
                    webview.setVisibility(WebView.GONE);
    
                    progressbar.setProgress(progress);
                    if(progress == 100) {
                        loadingtxt.setVisibility(LinearLayout.GONE);
                        progressbar.setVisibility(LinearLayout.GONE);
                        webview.setVisibility(WebView.VISIBLE);
                    }
                }
            });
    
            webview.setWebViewClient(new WebViewClient());
    
            byte[] post = EncodingUtils.getBytes("cmd=_s-xclick&hosted_button_id=VRM63MEY4J936", "BASE64");
            webview.postUrl("https://www.paypal.com/cgi-bin/webscr", post);         
    
        }
    
    
        @Override
        public void onConfigurationChanged(Configuration newConfig){        
            super.onConfigurationChanged(newConfig);
        }
    
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if(event.getAction() == KeyEvent.ACTION_DOWN){
                switch(keyCode)
                {
                case KeyEvent.KEYCODE_BACK:
                    if(webview.canGoBack() == true){
                        webview.goBack();
                    } else {
                        finish();
                    }
                    return true;
                }
            }
            return super.onKeyDown(keyCode, event);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater i = getSupportMenuInflater();
            i.inflate(R.menu.paypal_menu, menu);
            return super.onCreateOptionsMenu(menu);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle item selection
            switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return(true);
    
            case R.id.paypal_actionbar_reload:
                webview.reload();
                return(true);
            default:
                return super.onOptionsItemSelected(item);
            }
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   halfer Jatin Pandey    6 年前

    我在这里找到了答案:

    https://stackoverflow.com/a/9550231/1254514 “从Android 3.2(API级别13)开始,当设备在纵向和横向之间切换时,“屏幕大小”也会发生变化。”

    所以我不得不做以下事情:

    将您的Android清单添加到您喜欢的活动中:

    android:configChanges="keyboardHidden|orientation|screenSize"
    

    然后覆盖活动中的函数:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
    
        2
  •  0
  •   Marco RS    13 年前

    也许你可以试试这样的方法:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        if(getSharedPreferences(RPSActivity.PREFS_NAME, 0).getBoolean(RPSActivity.FULLSCREEN, true)) this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
        setContentView(R.layout.paypal);
    
        initUI();
    
         if(savedInstanceState != null)
         {
          /** Restoring the web Ui state. */
           webView.restoreState(savedInstanceState);
         }
    
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState)
    {
      /** Saving the webview state. */
      webView.saveState(outState);
    }
    

    这是未经测试的,但似乎您没有保存网络视图的状态。我在Fragment中使用了这段代码,它似乎保存了Web视图的状态。