简体   繁体   English

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

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

I have a small webView in one of my layouts that act as a news feed from my website.Every time I changed rotation it would reload the web page. 我的一个布局中有一个小的webView,作为我网站的新闻源。每次我改变轮换它会重新加载网页。

so I edited the code to include a on config change and on restore state.Then adjusted my android manifest to include the config changes which from the dev site says to include orientation and screen size. 所以我编辑了代码以包含配置更改和恢复状态。然后调整我的Android清单,以包括配置更改,从开发站点说包括方向和屏幕大小。

This solves the problem of saving the state but in turn creates a new problem which is I have different size layouts for when its in landscape mode on certain devices. 这解决了保存状态的问题,但反过来又产生了一个新问题,即我在某些设备上以横向模式设置了不同大小的布局。 but with the manifest set how it is it overrides screen size. 但是使用清单设置它是如何覆盖屏幕大小的。 is there away to save the state but with out using screen size in my manifest. 是在那里保存状态,但没有在我的清单中使用屏幕大小。 so then i could use my landscape layout 那么我可以使用我的景观布局

here is my code for the orientation changes. 这是我的方向更改代码。

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

then my manifest file has 然后我的清单文件了

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

ive added a container an a web view fragment but now the view disappears when rotation is landscape. 我添加了一个容器和一个Web视图片段,但现在当旋转是横向时视图消失了。 then disappears completly when i switch back. 当我换回时,然后完全消失。 so i tried adding the save out state and savestate to it which loaded the webview when rotated to landscape but crashed the app when going back to portrait 所以我尝试添加保存输出状态和savestate到它加载webview时旋转到横向,但在回到肖像时崩溃的应用程序

Here's some sample code that places the WebView in the activity layout as a fragment. 下面是一些示例代码,它将WebView作为片段放在活动布局中。 In portrait mode, the view is centered, in landscape, it is positioned at the bottom. 在纵向模式下,视图居中,在横向中,它位于底部。 Both orientations use the same single WebView instance. 两个方向都使用相同的单个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 mode) 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 (landscape) res / layout-land / main.xml(landscape)

<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 RES /布局/ 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" 
      />

I think you can simply solve all you problems by adding android:configChanges for your activity in AndroidManifest.xml like this: 我认为您可以通过在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>

ok guys so ive sussed it using fragments for the Webview. 好的家伙,所以我怀疑它使用片段为Webview。 i made two fragments, one called webivew and one called webviewfragmentlandscape. 我制作了两个片段,一个叫做webivew,另一个叫做webviewfragmentlandscape。

then in the on create i loaded the both fragments but make the landscape one invisible until the device goes in to land scape using the config changes method i was already using. 然后在创建我加载了两个片段,但使景观一个看不见,直到设备进入使用我已经使用的配置更改方法的景观scape。 here is my new code if any body can improve on what ive doen it would be a big help 这是我的新代码,如果任何身体可以改善我所做的将是一个很大的帮助

this is before the 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);


        }

this is in the on create 这是在创造

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

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

and this is the fragment for portrait 这是肖像的片段

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

finally the fragment for landscape 最后是景观片段

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 启动时如何保存应用定向状态 - How to save app orientation State when launched 如何保存片段的状态 - How can I save the state of my Fragments 尽管使用了共享首选项,但如何保存我的 cardView 单击事件状态以保存其颜色 - How can I save my cardView click event state to save its colour in spite of using shared preferences 方向更改时,如何在GridView中保存位图图像的状态? - How do I save state for bitmap images within GridView when orientation changes? 更改屏幕方向时如何控制android webview活动的更改? - How can I control android webview activity changes when I change screen orientation? 如何保存程序的状态然后加载它? - How can I save the state of my program and then load it? 如何在我的 android 应用程序中保存夜间模式的状态? - How can I save the state of the night mode in my android app? 关闭应用程序时如何保存活动状态? 即“网络视图” - How to save activity state when closing an application? namely "webview" 如何防止Webview在更改方向时更改文本大小? - How to prevent webview from changing text size on orientation change? 如何保存JCheckBox的状态 - How can i save state of JCheckBox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM