2013-10-22 15 views
5

Tôi nhận thấy có rất nhiều câu hỏi liên quan đến vấn đề này nhưng tôi đã thử tất cả các giải pháp để phát triển bản địa và không thể khiến chúng hoạt động trong Xamarin.Ngăn Android WebView tải lại trang khi xoay trong môi trường Xamarin

Tôi có một chế độ xem web được tải bằng URL chứa giao diện người dùng đồ họa để vẽ. Khi thiết bị được xoay, chế độ xem web sẽ tải lại trang và công việc của người dùng bị mất.

Các bài viết trên blog sau đây cung cấp cho các giải pháp tốt nhất cho điều này và giải thích rằng các giải pháp ghi nhận rộng rãi không sufficicient vì lý do sau:

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

Vấn đề chính với thực hiện này là, bất cứ khi nào bạn xoay màn hình,> WebView được tạo lại vì hoạt động bị hủy và phương thức saveState của nó không> lưu trạng thái đầy đủ, nhưng chỉ một phần của nó giống như URL của trang đã được tải và lịch sử duyệt web. Vì vậy, điều đó xảy ra, ví dụ: thu phóng và vị trí cuộn không được giữ nguyên sau khi thay đổi hướng màn hình và đôi khi trang được tải lại từ web.

Vì vậy, tôi đã triển khai giải pháp của mình, chuyển giải pháp từ Java sang Xamarin C#. Có vẻ hứa hẹn nhưng bất cứ khi nào hoạt động của tôi tải lại và InitUi được gọi, biến lớp web_view luôn luôn là null trong khi ví dụ dựa trên biến lớp này giữ giá trị của nó giữa các vòng xoay thiết bị.

Bất kỳ ai cũng có ý tưởng tại sao web_view mất giá trị của nó?

Đây là mã của tôi giống như bài viết nhưng được chuyển đến 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

Bạn có thực hiện các thay đổi đối với AndroidManifest.xml được đề cập trong bài viết được liên kết không? –

+0

Vâng, AndroidManifest.xml của tôi là như sau

Trả lời

4

Không thay đổi AndroidManifest.xml và sử dụng các thuộc tính tùy chỉnh.

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

Giải pháp này đã làm việc với tôi.

Các vấn đề liên quan