2013-10-22 88 views
5

我意識到有很多與此有關的問題,但我嘗試了所有本地開發的解決方案,並且無法讓它們在Xamarin下工作。防止Android WebView在Xamarin環境中旋轉重新加載頁面

我有一個web瀏覽器加載了一個URL,其中包含一個用於繪圖的圖形用戶界面。當設備旋轉時,webview會重新加載頁面,用戶的工作將會丟失。

下面的博客文章給出了最好的解決方案,這一點,並解釋說,廣泛的記錄解決方案是不sufficicient原因如下:

http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/

這個實現的主要問題是,每當你旋轉屏幕,> WebView被再次創建,因爲活動被銷燬,並且其saveState方法不會>保存完整狀態,但只有一部分類似於加載的頁面的URL和>瀏覽歷史記錄。因此,例如在屏幕方向改變之後,縮放和滾動位置不被保留,有時頁面從網上重新加載。

所以我已經實現了他們的解決方案,將它從Java移植到Xamarin C#。它看起來很有希望,但每當我的活動重新加載並調用InitUi時,web_view類變量始終爲空,而示例依賴於此類變量在設備旋轉之間保持它的值。

任何人有任何想法爲什麼web_view失去其價值?

這裏是我的代碼,這是完全一樣的文章,但移植到Xamarin C#。

public class EbookViewerActivity : ActionBarActivity 
{ 
    protected WebView web_view; 
    protected FrameLayout webViewPlaceholder; 

    Button loadButton; 
    Button downloadButton; 
    EditText testUrlText; 
    private string viewerPath; 

    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     #region Set up activity and action bar 

     //Create the activity screen and initialise the action bar 
     base.OnCreate(savedInstanceState); 
     RequestWindowFeature(WindowFeatures.NoTitle); 
     SetContentView(Resource.Layout.Viewer); 
     InitializeActionBar(); 

     //Set action bar button delegates 
     ActionBar 
      .AddLeftAction(new DelegateAction(Finish, Resource.Drawable.CloseIcon)) 
      .SetTitle("Ebook Annotator"); 

     //Set action bar logo 
     ActionBar.SetHomeLogo(Resource.Drawable.AcmeTrainingLogo); 
     #endregion 

     InitUi(); 
    } 

    private void InitUi() 
    { 
     Logger logger = Logger.Instance; 
     // Retrieve UI elements 
     webViewPlaceholder = FindViewById<FrameLayout>(Resource.Id.webViewPlaceholder); 
     // Initialize the WebView if necessary 
     if (web_view == null) 
     { 
      web_view = new WebView(this); 
      web_view.Id = Resource.Id.ebookDynamicWebView; 

      //web_view = FindViewById<WebView>(Resource.Id.ebookWebview); 
      web_view.Settings.JavaScriptEnabled = true; 
      web_view.AddJavascriptInterface(new AnnotationApiProxy(this), "AnnotationApi"); 
      string ebookId = Intent.GetStringExtra("ebookId"); 
      string userEmail = Intent.GetStringExtra("userEmail"); 
      Ebook ebook = EbookManager.GetEbook(int.Parse(ebookId)); 

      GlobalVariableHolder.Instance.EbookToOpen = int.Parse(ebookId); 
      viewerPath = "file:///android_asset/Annotator/annotator.html"; 

      web_view.Settings.AllowFileAccess = true; 
      logger.WriteToLog("Loading test harness with ebook id: " + ebookId, LogEntryLevel.Message); 
      web_view.SetWebChromeClient(new EbookWebViewClient() { }); 
      // web_view.SetWebViewClient(new WebViewClient()); 
      AnnotationDownloader annotationDownloader = new AnnotationDownloader(); 
      // annotationDownloader.XmlDownloaded += (sender, args) => RunOnUiThread(() => 
     // { 
       web_view.LoadUrl(viewerPath); 
      // }); 
      annotationDownloader.GetLatestEWorkBookXml(ebook.ID, userEmail); 
     } 

     // Attach the WebView to its placeholder 
     webViewPlaceholder.AddView(web_view); 
    } 

    protected override void OnSaveInstanceState(Bundle outState) 
    { 
     base.OnSaveInstanceState(outState); 
     // Save the state of the WebView 
     web_view.SaveState(outState); 
    } 

    protected override void OnRestoreInstanceState(Bundle savedInstanceState) 
    { 
     base.OnRestoreInstanceState(savedInstanceState); 
     // Restore the state of the WebView 
     web_view.RestoreState(savedInstanceState); 
    } 

    public override void OnConfigurationChanged(Configuration newConfig) 
    { 
     if (web_view != null) 
     { 
      // Remove the WebView from the old placeholder 
      webViewPlaceholder.RemoveView(web_view); 
     } 
     base.OnConfigurationChanged(newConfig); 
     // Load the layout resource for the new configuration 
     SetContentView(Resource.Layout.Viewer); 
     // Reinitialize the UI 
     InitUi(); 
    } 
} 
+0

您是否對鏈接文章中提到的AndroidManifest.xml進行了更改? –

+0

是的,我的AndroidManifest.xml是如下 <活動 機器人:名字= 「EbookViewerActivity。」 機器人:configChanges = 「鍵盤| keyboardHidden |屏幕尺寸|方向」 機器人:標籤= 「Eworkbook查看器」/> < /應用> –

回答

4

不要改變AndroidManifest.xml中並使用自定義屬性。

[Activity(ConfigurationChanges=ConfigChanges.Orientation | ConfigChanges.ScreenSize)] 
    public partial class MyActivity : Activity { ... 

該解決方案與我合作。