使用 webviewfragment 更改方向时如何保存 webview 的状态

how can i save the state of my webview when changing orientation using a webviewfragment

我的其中一个布局中有一个小的 webView,它充当我 website.Every 更改旋转时的新闻源,它会重新加载网页。

所以我编辑了代码以包含配置更改和恢复 state.Then 调整了我的 android 清单以包含开发站点所说的包括方向和屏幕尺寸的配置更改。

这解决了保存状态的问题,但反过来又产生了一个新问题,即当它在某些设备上处于横向模式时,我有不同大小的布局。但是随着清单的设置,它是如何覆盖屏幕尺寸的。是否可以保存状态但不在我的清单中使用屏幕大小。这样我就可以使用横向布局

这是我的方向更改代码。

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

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_homescreen);
    if (savedInstanceState != null)
        ((WebView)findViewById(R.id.neweview)).restoreState(savedInstanceState);
    svHomeScreen = (ScrollView) findViewById(R.id.svHomeScreen);
    svHomeScreen.setScrollbarFadingEnabled(false);

    wv = (WebView) findViewById(R.id.neweview
    );
    wv.setScrollbarFadingEnabled(false);

    wv.getSettings().setJavaScriptEnabled(true);
    wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    wv.getSettings().setRenderPriority(WebSettings.RenderPriority.NORMAL);
    wv.getSettings().setLoadsImagesAutomatically(true);
    wv.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

    if (Build.VERSION.SDK_INT >= 19) {
        // chromium, enable hardware acceleration
        wv.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    } else {
        // older android version, disable hardware acceleration
        wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    wv.canGoBackOrForward(5);
    wv.getSettings().setLoadWithOverviewMode(true);
    wv.getSettings().setUseWideViewPort(true);
    wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);


    wv.setPadding(0, 0, 0, 0);
    wv.loadUrl(NEWS);

然后我的清单文件有

 <activity
        android:name=".Homescreen"
        android:label="@string/title_activity_homescreen"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        />

我添加了一个容器和一个 web 视图片段,但现在当旋转为横向时视图消失。然后当我切换回来时完全消失。所以我尝试向其中添加保存状态和保存状态,这在旋转到横向时加载了 webview 但在返回纵向时使应用程序崩溃

我认为您可以通过在 [=17= 中为您的 activity 添加 android:configChanges 来简单地解决所有问题]像这样:

    <activity
        android:name=".activities.MyActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label=""
        android:theme="@style/MyTheme"
        android:parentActivityName=".activities.Home">
    </activity>

好吧,我已经使用 Webview 的片段怀疑了它。我制作了两个片段,一个叫webivew,一个叫webviewfragmentlandscape。

然后在创建时我加载了两个片段,但使景观一个不可见,直到设备使用我已经使用的配置更改方法进入景观。这是我的新代码,如果任何人可以改进我所做的,那将是一个很大的帮助

这是在 oncreate 之前

 @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    View Sidepane = findViewById(R.id.NewsFeed);
    View Squarepane = findViewById(R.id.NewsFeed2);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();


        if (Sidepane.getVisibility() == View.VISIBLE) {
            Sidepane.setVisibility(View.GONE);}

        Squarepane.setVisibility(View.VISIBLE);


    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();

        if (Squarepane.getVisibility() == View.VISIBLE) {
            Squarepane.setVisibility(View.GONE);

            Sidepane.setVisibility(View.VISIBLE);


        }

这是在创建中

 View Sidepane = findViewById(R.id.NewsFeed);
    View Squarepane = findViewById(R.id.NewsFeed2);

    Squarepane.setVisibility(View.GONE);
    Sidepane.setVisibility(View.VISIBLE);

这是肖像的片段

 <<?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">


   <WebView
   android:layout_width="match_parent"
   android:layout_height="240dp"
   android:id="@+id/NewsFeedWbview"
   android:layout_gravity="center_vertical"
   android:layout_alignParentBottom="true"
   android:layout_alignParentLeft="true"
   android:layout_alignParentStart="true">

   </WebView>
 </RelativeLayout>

最后是风景片段

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

   <WebView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/NewsFeedWbview2"

    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="bottom">

   </WebView>
 </LinearLayout>

下面是一些将 WebView 作为片段放置在 activity 布局中的示例代码。在纵向模式下,视图居中,在横向模式下,它位于底部。两个方向都使用同一个 WebView 实例。

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

        // load the fragment - we only need to do this when the activity is first created
        // - not on a recreation due to screen rotation, etc
        if (savedInstanceState == null) {
            FragmentManager fm = this.getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.container, new WebViewFragment());
            ft.commit();
        }

    }

    // this could go in a separate file
    public static class WebViewFragment extends Fragment {
        WebView mWebView;

        @Override
        public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
            View v = layoutInflater.inflate(R.layout.webviewfrag, null, false);
            this.mWebView = (WebView)v.findViewById(R.id.NewsFeedWbview);
            this.mWebView.loadUrl("https://google.com");
            return v;
        }

    }
}

res/layout/main.xml(default/portrait模式)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <!-- container layout for the webview fragment
    - note the id for this view is the same in both this and the landscape version -->
   <FrameLayout
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="240dp"
   android:layout_centerVertical="true"
    />

</RelativeLayout>

res/layout-land/main.xml(横向)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <!-- container layout for the webview fragment
    - note the id for this view is the same in both this and the portrait version -->
   <FrameLayout
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="100dp"
   android:layout_alignParentBottom="true"
   />

</RelativeLayout>

res/layout/webviewfrag.xml

<!-- make the WebView fill its container -->
    <WebView
      xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/NewsFeedWbview"
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
      />