如何在使用WebViewFragment时改变屏幕方向后保存WebView的状态

6

我在一个布局中有一个小的webView,作为来自我的网站的新闻源。每次更改旋转时,它都会重新加载网页。

因此,我编辑了代码,包括on config change和on restore state。然后调整了我的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视图片段,但是当屏幕旋转为横向时,视图会消失。然后在切换回来时完全消失。所以我尝试添加了保存状态并将其保存到out状态中,这样在旋转到横向时可以加载Web视图,但是当返回到纵向时却导致应用程序崩溃。


你尝试在 <activity> 标签(清单)中添加 android:configChanges="orientation|screenSize" 吗? - Omar Aflak
是的,我尝试过这样做并且它保存了状态,但它覆盖了我的屏幕尺寸,这就是我的问题。如果我删除|screensize选项,它会恢复到不保存状态。 - Mark Harrop
2
你可以尝试将它放在一个Fragment中 - 当屏幕旋转时,Fragment不像Activity那样被彻底重建。你可能会发现,位于Fragment内部的WebView在旋转后仍然完好无损。 - adelphus
有人可以向我展示如何将其编写为片段吗?因为我从未使用过。 - Mark Harrop
谢谢@adelphus,这个片段正是我需要的。我会回答下面的问题。 - Mark Harrop
3个回答

2

这是一段示例代码,将WebView作为片段放置在活动布局中。在竖屏模式下,视图位于中心,在横屏模式下,它位于底部。两种方向都使用同一个单独的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(默认/纵向模式)

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

谢谢@adelphus,我现在会尝试类似的东西。 - Mark Harrop
@adelphus,我昨晚尝试了这个方法,但当我旋转屏幕时,视图完全消失了。还有其他的尝试方法吗? - Mark Harrop
我也尝试过在webviewFragment中添加保存状态和savestate,这样可以使webview出现在两个视图中。但是它不能保存状态,并且在返回到纵向时会导致应用程序崩溃。 - Mark Harrop

0

我认为你可以通过在AndroidManifest.xml中为你的活动添加android:configChanges来简单地解决所有问题,像这样:

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

0

好的,伙计们,我已经使用Webview片段解决了问题。我创建了两个片段,一个名为webivew,另一个名为webviewfragmentlandscape。

然后在oncreate中,我加载了这两个片段,但是将横向的片段设置为不可见,直到设备进入横向模式,使用我已经使用的配置更改方法。如果有人能改进我的代码,那将是一个很大的帮助。

这是在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);


        }

这是在onCreate中

 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>

1
你所做的可以工作,但不是理想的,因为你加载了两个片段来完成同一件事情,这是不必要的。通常,您需要为Activity创建两个布局(一个普通布局,一个横向布局),每个布局都有自定义的布局大小,然后在onCreate()期间将单个片段(带有您的webview)膨胀/加载到其中。当屏幕旋转时,Android会为Activity加载备用布局,但会自动将片段转移到其新的布局容器中(两个布局中的ID必须匹配),而无需进行其他任何操作。 - adelphus
如果那不太清楚,我会尝试创建一个示例来说明我认为你需要什么。 - adelphus
这太棒了,因为我在尝试调整大小时遇到了问题,虽然它能工作,但就像你所说的,我知道有更好的方法。 - Mark Harrop

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接