0

我知道有很多类似的问题,相信我,我尝试了很多,但对我来说没有任何效果。我有一个使用 Webview 的 Android 应用程序。您可以在 Play 商店中找到我的应用程序:

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

在 Play 商店呆了几个月后,我决定进行更新,并为我的新设计使用 Android 设计指南,因此操作栏是必要的。我选择了 ActionbarSherlock,一切都很好,除了 Webview 现在每次我旋转手机时都会重新加载。在我的上一个版本中,我用这个答案处理了这个问题并且它有效:

当我从纵向更改为横向时,我的 Webview 会重新加载

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

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

同样令人失望的结果。我什至不确定 ActionbarSherlock 是否是问题所在,因为我已经更改了很多,但是无法重新创建旧状态!

这是应用程序的外观(仅在新版本中实现,您无法在 Play Store 上发布的版本中找到它):

载入画面

贝宝网站

这是我的代码的一部分,首先是清单,然后是布局,然后是 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);
        }
    }
}
4

2 回答 2

2

我在这里找到了答案:

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);
}
于 2012-10-07T18:25:42.350 回答
0

也许如果你尝试这样的事情:

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

这是未经测试的,但似乎您没有保存 webview 的状态。我在片段中使用此代码,它似乎保存了 webview 的状态。

于 2012-09-14T00:31:51.523 回答