[英]how can i save the state of my webview when changing orientation using a webviewfragment
我的一個布局中有一個小的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視圖片段,但現在當旋轉是橫向時視圖消失了。 當我換回時,然后完全消失。 所以我嘗試添加保存輸出狀態和savestate到它加載webview時旋轉到橫向,但在回到肖像時崩潰的應用程序
下面是一些示例代碼,它將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(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 /布局/ 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"
/>
我認為您可以通過在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>
好的家伙,所以我懷疑它使用片段為Webview。 我制作了兩個片段,一個叫做webivew,另一個叫做webviewfragmentlandscape。
然后在創建我加載了兩個片段,但使景觀一個看不見,直到設備進入使用我已經使用的配置更改方法的景觀scape。 這是我的新代碼,如果任何身體可以改善我所做的將是一個很大的幫助
這是在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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.