Created
June 5, 2017 17:25
-
-
Save cleidimarviana/c269a626f92cefa15c262a6de271d62b to your computer and use it in GitHub Desktop.
Salvando estado do WebView usando o método savedInstanceState
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.webkit.WebSettings; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
public class MainActivity extends Activity { | |
WebView myWebView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
myWebView = (WebView)findViewById(R.id.myWebView); | |
if (savedInstanceState != null){ | |
myWebView.restoreState(savedInstanceState); | |
} else { | |
myWebView.loadUrl("http://meulink.com/"); | |
myWebView.setWebViewClient(new MyWebViewClient() { | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
view.loadUrl(url); | |
return false; | |
} | |
}); | |
WebSettings webSettings = myWebView.getSettings(); | |
webSettings.setJavaScriptEnabled(true); | |
} | |
} | |
// Use When the user clicks a link from a web page in your WebView | |
private class MyWebViewClient extends WebViewClient { | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
if (Uri.parse(url).getHost().equals("http://meulink.com/")) { | |
return false; | |
} | |
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | |
startActivity(intent); | |
return true; | |
} | |
} | |
@Override | |
protected void onSaveInstanceState(Bundle outState) { | |
myWebView.saveState(outState); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment