【问题标题】:how can i save the state of my webview when changing orientation using a webviewfragment使用 webviewfragment 更改方向时如何保存 webview 的状态
【发布时间】:2016-05-10 17:48:10
【问题描述】:

我的一个布局中有一个小的 webView,它充当我网站的新闻提要。每次我更改旋转时,它都会重新加载网页。

所以我编辑了代码以包含配置更改和恢复状态。然后调整我的 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 视图片段,但现在当旋转为横向时视图消失了。然后当我切换回来时完全消失。所以我尝试向其中添加保存状态和保存状态,当旋转到横向时加载 web 视图,但在返回纵向时应用程序崩溃

【问题讨论】:

  • 您是否尝试在您的&lt;activity&gt; 标签(清单)中添加android:configChanges="orientation|screenSize"
  • 是的尝试过,它保存了状态,但它覆盖了我的屏幕尺寸,这是我的问题,如果我删除 |screensize 选项它会恢复为不保存状态
  • 您可以尝试将其放在片段中 - 当屏幕旋转时,片段不会像活动一样残酷地重新创建。您可能会发现片段中的 webview 完好无损地在旋转中幸存下来。
  • 有人可以告诉我如何将它写成一个片段,因为我从未使用过片段
  • 感谢@adelphus 这个片段是我需要的,我会回答下面的问题

标签: java android savestate


【解决方案1】:

下面是一些将 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 现在会尝试这样的事情
  • @:adelphus 我昨晚试过了,但是当我旋转屏幕时,视图完全消失了。还有什么可以尝试的
  • ive 还尝试将保存状态和保存状态添加到 webviewFragment。这使得 webview 出现在两个视图中。但是在返回纵向时不会保存状态并且还会使应用程序崩溃
【解决方案2】:

我认为您可以通过在 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>

【讨论】:

    【解决方案3】:

    好吧,伙计们,我用 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>
    

    【讨论】:

    • 您所做的将起作用,但这并不理想,因为您为同一件事加载了 2 个片段,这应该是不必要的。通常,您为 Activity 创建两个布局(一个普通布局,一个横向布局),每个布局具有所有自定义布局大小,然后在 onCreate() 期间将 single 片段(使用您的 webview)膨胀/加载到其中。当屏幕旋转时,Android 会为 Activity 加载备用布局,但会自动将 Fragment 传输到其新的布局容器(两个布局中的 id 必须匹配),而无需您执行任何其他操作。
    • 如果这没有意义(!),我会尝试创建一个我认为您需要的示例。
    • 那会很棒,因为我遇到了尺寸问题,这样做它可以工作,但就像你说的那样,我知道有更好的方法
    猜你喜欢
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    相关资源
    最近更新 更多