2017-10-20 1 views
0

나는 앱을 만들고있다 Xamarin android 사용자가 탐색하는 곳. 앱에 webview가 포함되어 있습니다. 사용자가 webview를 열면 url이로드되고 찾아 볼 수 있습니다. 사용자가 앱을 종료하고 다시 열면 마지막으로 방문한 URL을 보는 대신 URL이 다시로드됩니다.방문 마지막로드 된 URL

나는 여기서 무엇을 잘못하고 있는지 모른다.

protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 
     webView = FindViewById<WebView>(Resource.Id.webView1); 
     webView.SetWebViewClient(new MyWebClient()); 
     CookieManager.Instance.SetAcceptCookie(true); 
     webView.Settings.JavaScriptEnabled = true; 
     webView.Settings.SetAppCacheEnabled(true); 
     webView.LoadUrl(getUrl()); 
     webView.SetPadding(0, 0, 0, 0); 


     webView.Settings.SetSupportZoom(true); 

    } 
    public void saveUrl(String url) 
    { 
     ISharedPreferences sp = GetSharedPreferences("SP_WEBVIEW_PREFS", FileCreationMode.Private); 
     ISharedPreferencesEditor editor = sp.Edit(); 
     editor.PutString("SAVED_URL", url); 
     editor.Commit(); 
    } 
    public String getUrl() 
    { 

     ISharedPreferences sp = GetSharedPreferences("SP_WEBVIEW_PREFS", FileCreationMode.Private); 
     //If you haven't saved the url before, the default value will be google's page 
     return sp.GetString("SAVED_URL", "http://google.com"); 

    } 
    public void onPageFinished(WebView view, String url) 
    { 
     this.onPageFinished(view, url); 
     saveUrl(url); 

    } 
} 

internal class MyWebClient : WebViewClient 
{ 
    public override bool ShouldOverrideUrlLoading(WebView view, string url) 
    { 
     view.LoadUrl(url); 
     return false; 
    } 
} 
+0

적절한 제목을 쓰고 "URL이 다시로드되었습니다"라는 질문을 수정해야합니다. 처음부터 시작한다는 의미입니까? –

+0

예 처음부터 시작합니다 –

답변

0

당신은 활동에 onPageFinished 방법에 놓여있다. 그것은 다음과 같이 MyWebClient 클래스에서 재정의해야합니다 URL이로드를 완료 한 후이 방법에로드 된 URL을 저장할 때

internal class MyWebClient : WebViewClient 
{ 
    public override bool ShouldOverrideUrlLoading(WebView view, string url) 
    { 
      view.LoadUrl(url); 
      return false; 
    } 


    public override void OnPageFinished(WebView view, String url) 
    { 
      base.OnPageFinished(view, url); 

      //Save the url here. 
      //This method itself gives you the last url loaded as it's url Parameter. 
      ISharedPreferences sp = Application.Context.GetSharedPreferences("SP_WEBVIEW_PREFS", FileCreationMode.Private); 
      ISharedPreferencesEditor editor = sp.Edit(); 
      editor.PutString("SAVED_URL", url); 
      editor.Commit(). 
    } 
} 

이 메소드는 자동으로 호출됩니다.

+0

답장을 보내 주셔서 감사합니다. 저장 URL에 webview.URL()을 사용할 수 있습니까 ??? –

+0

귀하의 활동에 작성된 saveUrl 메서드를 사용해야한다고 생각하지 않습니다. 코드를 OnPageFinished 메서드 자체로 이동하고 거기에서 마지막 URL을 저장하는 것이 좋습니다. 원하는 경우 webView.Url을 사용할 수 있지만 OnPageFinished 메서드 매개 변수의 일부로 제공됩니다. 더 나은 활동에서 saveUrl 메서드를 제거하고 내 편집 된 답변을 확인하십시오. – MilanG

관련 문제