Launching new activity resets ViewPager - android

I have a ViewPager with each view being a WebView. In the Actionbar I have a menu item, where the user can open the web page in their default browser using this code:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(mUrl)));
However, if a user clicks on a link in the WebView, then opens the site in their default browser, then goes back to my app, they are shown the original web page, not the page they clicked to. I thought about using startActivityForResult instead of startActivity, but that would only work if I opened a new activity in my own app, right?
This is the code for my ViewPager:
Fragment
public class Browser extends SherlockFragment {
private String mUrl;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
activity = getActivity();
setHasOptionsMenu(true);
mUrl = GetUrl();
mWebView = (WebView)getView().findViewById(R.id.webview);
mWebView.loadUrl(mUrl);
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(final MenuItem item)
{
if (item.getItemId() == R.id.open) {
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(mUrl)));
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
}
Activity
public class BrowserPager extends SherlockFragmentActivity implements Interfaces.OnBrowserSetTitle {
#Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.browser_pager);
}
}

Nevermind, I'm an idiot. I was resetting the ViewPager in onResume of the Activity, which was causing the webpage to reset. Moving the code to reset the ViewPager to onCreate fixed my issue.

Related

How can i add a horizontal progress bar on top of web-view?

I am trying to implement a progress bar at the top of my web-view while the page loads. Just like chrome.
How can i do this?
i have a swipe refresh view in a container.
I've looked at a lot of answers here and tried to implement them, but had no luck.
I just want to implement a simple progress bar (like chrome) every time the web-view loads.
public class MainActivity extends Activity {
WebView browser;
SwipeRefreshLayout mySwipeRefreshLayout;
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// find the WebView by name in the main.xml of step 2
browser=(WebView)findViewById(R.id.webview);
// Enable javascript
browser.getSettings().setJavaScriptEnabled(true);
// Set WebView client
browser.setWebChromeClient(new WebChromeClient());
browser.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// open in Webview
if (url.contains("example.com") ){
// Can be clever about it like so where myshost is defined in your strings file
// if (Uri.parse(url).getHost().equals(getString(R.string.myhost)))
return false;
}
// open rest of URLS in default browser
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
public void onPageFinished(WebView view, String url) {
mySwipeRefreshLayout.setRefreshing(false);
}
});
// Load the webpage
browser.loadUrl("https://example.com/");
mySwipeRefreshLayout = this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
browser.reload();
}
}
);
}

facebook like on webview android shows blank screen after login

I utilize the facebook html like for my app to open a webview like this https://developers.facebook.com/docs/plugins/like-button/
The webview shows a Like and Share button, but after I login to facebook, it doesnt return to the Like and Share button, but a blank page, the share button works fine.
So how do I return to the facebook like url after logging in?
public class LikeFacebookActivity extends BaseActivity {
private WebView webView;
private final String URL = "facebookIDhere";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.like_facebook_webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
showLoading();
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(URL);
ActionBar actionbar = getActionBar();
actionbar.setCustomView(R.layout.actionbar_top_like_facebook);
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Button backButton = (Button) findViewById(R.id.buttonGeneralBack);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
#Override
public void onBackPressed() {
finish();
overridePendingTransition(R.anim.animation_slide_from_left,
R.anim.animation_slide_to_right);
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("something")) return true;
return false;
}
public void onPageFinished(WebView view, String url) {
hideLoading();
}
}
}
I have solved this, just use system.out.println to see which page does facebook load after loggin in
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("something")) return true;
return false; //Default is to not override unless our condition is met.
}
public void onPageFinished(WebView view, String url) {
hideLoading();
//String webUrl = webView.getUrl();
//System.out.println(webUrl);
if(url.startsWith("https://www.facebook.com/plugins/close_popup.php#_=_")){
String redirectUrl = URL;
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}

Android Webview shows error in emulator

i have a problem with loading a webview from a string url.
here is the code of an activity with only WebView.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
Intent intent=getIntent();
String url=intent.getStringExtra("url");
//EditText edit=(EditText)findViewById(R.id.editText1);
//String url=edit.getText().toString();
WebView webview=(WebView)findViewById(R.id.webView1);
webview.loadUrl(url);
webview.setWebViewClient(new WebViewClient());
}`
and code of the calling activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onGo(View v)
{
EditText edit=(EditText)findViewById(R.id.editText1);
Intent intent=new Intent(MainActivity.this,WebActivity.class);
intent.putExtra("url", edit.getText().toString());
startActivity(intent);
}
i have even added the android.permissions.INTERNET in the manifest
but still i cannot view the page with the click of the button in MainActivity.
From the comment
yes the real device has the same error with "www.google.com"
Use
http://www.google.com
Also make sure you have internet permission mentioned in manifest file
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

how to set a progress Dialog when a browser is being launched

I have come up with a private browser,but I want to know how to link it to my code below when the browser is being opened.
Main Class
b5.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent i =new Intent(FiltersActivity.this,MyBrowser.class);
i.setData(Uri.parse("http://www.google.com"));
startActivity(i);
}
});
Second Class
public class MyBrowser extends Activity {
public void onCreate(Bundle SavedIntstanceState) {
super.onCreate(SavedIntstanceState);
setContentView(R.layout.browser);
Uri uri = getIntent().getData(); // get Intent and the linked data
WebView webview = (WebView) findViewById(R.id.webView_01); // get the
// webview
// item
webview.setWebViewClient(new Callback()); // set the object to the view
// to webview
webview.loadUrl(uri.toString()); // load the url
}
private class Callback extends WebViewClient { // crate a virtual class
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) { // /load
// the
// necessary
return (false);
}
}
}
You can check out this blog with complete example that shows progess of the webview in the Title Bar.
Also you can have a look in the Android Docs activity.setProgress(progress)
And finally but not the least StackOverflow Answer

Android : Simple web application crashes on links other then the http scheme

I am a beginner in developing for the Android, so excuse me if this is obvious. I created a simple web application using the samples from android.com however, when a link like tel: or mailto: is clicked, the application crashes and askes me to force quit. I was wondering if anyone could tell me why.
public class MyApplication extends Activity {
WebView mWebView;
private class InternalWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// This is my web site, so do not override; let my WebView load the page
if (Uri.parse(url).getHost().equals("m.mydomain.nl")) {
return false;
} else {
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the WebView class to display website */
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new InternalWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://m.mydomain.nl/");
}
}
Sorry to be so stupid, this actualy fixed it. With mailto, there is no host, thus getHost() will return null and causes the exception.
private class InternalWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// This is my web site, so do not override; let my WebView load the page
if (Uri.parse(url).getHost() != null && Uri.parse(url).getHost().equals("m.domain.nl")) {
return false;
} else {
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}

Categories

Resources