Detect when URL changes in WebView - Xamarin - android

It is my first time working with Xamarin, and need to show up a back button on the screen only when the URL of my WebView is different from the first one, so that the users can go back to the "home" page of the web site. If there is any function that gets called once the page is completely loaded so I can get the URL and compare with the previos one would be great. Thanks you!

You could get the url from the event webView.Navigated
webView.Navigated += WebView_Navigated;
private void WebView_Navigated(object sender, WebNavigatedEventArgs e)
{
var url = e.Url;
//...
}

Related

Android WebView with AngularJS application route changes not being detected in shouldOverrideUrlLoading

I have been working on a hybrid android application. Currently a WebView in our application is pointing to an AngularJS 1.5.7 application. When the user hits a button inside of the application that changes the route I was expecting the shouldOverrideUrlLoading function to be called inside of my WebViewClient. However, this is not the case. It looks like shouldOverrideUrlLoading does not get hit on Angualar route changes.
This being the case I have gone down the following rabbit holes:
onPageFinished - Overriding this function in the WebViewClient works, however, it is not being called until after the new route is getting hit. Which is adding to the application loading time and creating a choppy experience. ` #Override
public void onPageFinished(WebView view, String url) {
if (url.endsWith("/#/")) {
signOut();
} else if (url.endsWith("/login")) {
// TODO: show some sort of failure message?
Log.i("Login Route", "The webview just attempted to go to the login route.");
signOut();
} else if (url.endsWith("/security")) {
Intent intent = new Intent(getApplicationContext(), SecurityActivity.class);
startActivity(intent);
}
}`
shouldInterceptRequest - Overriding this function allows you to watch for requests. However, by the time the requests go out from the AngularJS application the web view is showing a new route once again providing a choppy user experience.
onLoadResource - same
JavaScriptInterface - Currently I have set up a JavaScript interface to watch for window.location changes. This seems to catch the route changes quicker than any of the above options, however, there is still a glimpse quick flicker of the web page I do not want to do go to. You can find how to do Javascript bridging on this post
Any suggestions would be greatly appreciated! Thanks.

Android: Get the clicked url that opened the application

I have App Links setup which routes the specific urls into the application.
How can I determine what the url of the last clicked url that opened the app was?
The issue I'm running into is if multiple listeners for onResume are registered (IE I open the app from different sources a url click from Safari and messages), I get all of the previously clicked urls called.
public void onActivityResumed(Activity activity) {
String currentLink = activity.getIntent().getDataString();
}
Essentially I'm looking for the Android equivalent of the following iOS code to get the url which opened up the app.
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
NSURL *clickedURL = userActivity.webpageURL;
}

Cookie not saving in Web view in Xamarin(Android)

I am creating an android application in xamarin. I am using a web view to display a website. After user login website create a cookie and should store in web view. There are two web view in app, One web view is displaying the pages and if there is any text box in the page that page is opened in second web view.
So now when user tries to login, second web view is opened(as login page contains text box), after user enter details and click next button, the second view is closed and next page is opened in first web view. After login a cookie is created and stored in web view and when user open the app next time its doesn't asks for login. This is what should happen.
The problem is, if the user enter details and after clicks the next button(next page is loading in first web view) and immediately quits the application then start app again then cookies does not exists and app asks for login again.
After login I am reading the cookie value on page finish event of webview and displaying in toast. If i quits the app after login, i gets the cookie value in toast but when i starts the app again the cookie doesn't exists anymore and it asks me for login again
public override void OnPageFinished (WebView view, string url)
{
try
{
if (view.Url == Urls.URL_INDEX)
{
var cookieManager = CookieManager.Instance;
if (cookieManager != null)
{
//getcookie string from the url
string cookie = cookieManager.GetCookie (view.Url);
if (!string.IsNullOrEmpty (cookie))
{
string[] cookies = cookie.Split (';');
foreach (var newcookie in cookies)
{
if (newcookie.Trim().StartsWith (Constants.COOKIE_NAME))
{
string cookieValue = newcookie.Substring (newcookie.IndexOf ('='));
Toast.MakeText(activity,cookieValue,ToastLength.Short).Show();
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine ("Exception in storing cookie in Home Activity : "+ex.Message);
Toast.MakeText (activity, "Exception : " + ex.Message,ToastLength.Long).Show();
}
}
I don't know why this is happening, please help.
The cookies are stored in RAM to get the best performance and synced every five minutes to the permanent storage. You'll need to manually force CookieSyncManager to sync the cookies in your OnPageFinished method so that they're still available when you start the application again. Refer to the CookieSyncManager documentation for more details.

Is it really posible to close a PhoneGap App?

I have searched all over the web and found different ways of closing a PhoneGap App. I tested all of them and none work. At least on Android.
Question:
Is it possible (By Feb 2014) to have a close button in a PhoneGap App on Android?
Thanks
This doesn't work:
function CloseApp() {
if (confirm('Close this App?')){
if (navigator.app) {
navigator.app.exitApp();
}else if (navigator.device) {
navigator.device.exitApp();
}
}
}
Is
navigator.app.exitApp()
really killing/closing the android app with phonegap?
I use cordova and have the same issue. Above mentioned code is just putting the app into background - I checked the running tasks (android task manager) after above code got executed by the app.
I am confused on why you want a button to close the app. Android already has a back button when clicked enough times will take the user back to the phone's main screen. There is also a home button that takes the user out of an app. Once, out of the app the user can "kill" the app through a task manager.
navigator.app.exitApp()
works and I use it in all my cordova apps. Check the rest of your code.
As ejwill said, having a "close" button is a bad idea. On Android I call exitApp when the user is the home page of my app and he presses the backbutton:
function onDeviceReady() {
document.addEventListener("backbutton", onBackKey, false);
}
function onBackKey( event ) {
var l = window.location.toString();
var parts = l.split('#/'); // this works only if you are using angularjs
var page = parts[1];
if (page == 'home') {
navigator.app.exitApp();
} else {
// do something else... one option is:
navigator.app.backHistory();
}
}
My 2c.

loading screen when window opening up: Android

i am using the Titanium studio for developing android application. a user click on item a new window is being opened which fetch data from a site and populate the tableview. so this window does take time to open.
mean while i am fetching the data and showing loading screen like:
anotherWind.addEventListener('open', function (e) {
activityIndicator.show();
setTimeout(function(){
e.source.close();
activityIndicator.hide();
}, 6000);
});
the problem is at this point i'm assuming it takes 6 second to fetch and display a tableview. but in real time it may not be the case as time may vary depending upon the data
when user click a icon it should display the loading screen only for the time data is not pulated and showed in tableview.
its a kind of notification between two tasks. one when task is complted it should notify other one.
how can i resolve that ?
You can use a custom event listener.
Example:
Ti.App.addEventListener('tableDataLoaded', function() {
activityIndicator.hide();
}
When your table data is loaded, you fire the event:
Ti.App.fireEvent('tableDataLoaded');
I hope this will help you :)
It seems multi-thread will be a good solution.Android provide some mechanisms of communication between different threads or processes.
i have implemented the same. my new window needs to load remote data and populate the tableview.
so i just show the indicator in window open
anotherWind.addEventListener('open', function (e) {
activityIndicator.show();
});
and then hide it when my remote data is loaded. inside the callback of httpclient
'APIGetRequest(this.apiURL, function(e) {
var status = this.status;
if (status == 200) {
populatetableview(this.responseText);
activityIndicator.hide();
}
});'

Categories

Resources