Android chrome custom tabs backstack problem - android

I am using chrome custom tabs to integrate a payment gateway inside an Android App. Inside the payment gateway I have have to provide payment_success_redirect url and payment_fail_redirect for fail.
Problem here is that whether payment succeeded or failed user is redirected to the given page. Now if user presses hardware back button previous page starts loading and I do not want this.
Is there a way to prevent this or know explicitly on which URL currently I am on.
My Code:
try {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.intent.setData(Uri.parse(url));
startActivityForResult(customTabsIntent.intent, CHROME_CUSTOM_TAB_REQUEST_CODE);
} catch (Exception e) {
e.printStackTrace();
}

The API of Custom Tab component do not let the host app retrieve on which URL the user is right now, for privacy matters.
You can monitor only some navigation events as described here.

Related

Disable swipe to refresh in chrome CustomTab

I'm using chrome CustomTab to open a url inside my app, using customTabsIntent.launchUrl method and CustomTabsIntent.Builder. Here's how I'm doing it right now:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(someUrl);
is there any way to disable the swipetorefresh behaviour that is set by default, in the CustomTab? by modifying the builder or something? (without making any changes to the website)
No, This is not currently possible, as Custom Tabs will respect the behaviour of the opened web page. If changing the page is possible, these instructions can help

Chrome custom tab crashes if chrome browser is disabled by the user in his device

I am trying to open pdf file in chrome custom tab, it works fine by using this code
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse("https://docs.google.com/viewerng/viewer?url=" +pdflink));
But the problem is, if the user have disabled chrome from app settings and there is no other browser it crashes. So how can i open it in the chrome custom tab if it is disabled.
Thanks in advance.
this is the exception that is showing up in the logcat.
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://docs.google.com/... (has extras) }
Try this:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
try {
customTabsIntent.launchUrl(this, Uri.parse("https://docs.google.com/viewerng/viewer?url=" +pdflink));
}
catch (ActivityNotFoundException e)
{
// display message to user
}
Handle your exception like below
try{
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse("https://docs.google.com/viewerng/viewer?url=" +pdflink));
}catch(ActivityNotFoundException e){
// Handle your exception
}
EDIT
If user has disabled it then you can ask user to enable it from settings.
Code for open phone settings
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
REASON behind this, can be easily searched from this SO post.
Written by CommonsWare
Chrome does not appear to export that activity, so it cannot be started by third-party apps.
Even if they did, that behavior could easily vary from version to version of Chrome for Android. Google does not document or support access to such activities from any of their commercial apps, let alone Chrome.

Handling External Links in android WebView like Gmail App does

I'm a web developer. I'm currently developing android application on Android Studio using WebView which access my website as an android application. One of my webpage contains many external links. My goal is to make the android application can handle external links like Gmail App does (also like facebook and Line do).
Below is the example of gmail app.
An email contains external link
Link clicked, then application open a new activity acts like a browser without leaving Gmail application
Any idea how to make it?
It is pretty simple. You have to use Chrome Custom Tabs as suggested by Gergely as well in comment. Below is the small functional code that will help you to achieve this.
First add this dependency to your build.gradle(Module:app)
compile 'com.android.support:customtabs:23.4.0'
Second add below function to your code and simply pass string URL to it.
private void redirectUsingCustomTab(String url)
{
Uri uri = Uri.parse(url);
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
// set desired toolbar colors
intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
// add start and exit animations if you want(optional)
/*intentBuilder.setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left,
android.R.anim.slide_out_right);*/
CustomTabsIntent customTabsIntent = intentBuilder.build();
customTabsIntent.launchUrl(activity, uri);
}
Rest it will take care itself. Since Chrome Custom Tabs can customised so lot can be done like you can add menu to toolbar. For detailed information you can visit official documentation from Google itself here.
Hope it will help you to start with :)

Open Facebook native app from your android app

My question is directly connected with this one Open Facebook page from Android app? Which answer (for the version at the moment) is not the marked one but this one
public static Intent newFacebookIntent(PackageManager pm, String url) {
Uri uri;
try {
pm.getPackageInfo("com.facebook.katana", 0);
// https://stackoverflow.com/a/24547437/1048340
uri = Uri.parse("fb://facewebmodal/f?href=" + url);
} catch (PackageManager.NameNotFoundException e) {
uri = Uri.parse(url);
}
return new Intent(Intent.ACTION_VIEW, uri);
}
Which is confirmed working last on 7th of February 2015.
My question is should you use the whole url (like www.facebook.com/mypage) or just add the url of the page so it'll be fb://facewebmodal/f?href=mypage. I tried both and it just opens the fb app without an actual page. It shows blank fb page on both tries.
Can someone give me an example with url for some public page that works?
You can try to write mypage as https://www.facebook.com/ID. The whole uri should then be
fb://facewebmodal/f?href=https://www.facebook.com/ID
and change ID to the page you want to visit.
That functionality is not documented or supported, so you may get unknown result. You may want to try passing the Page ID or Profile ID instead of names. You can get the ID by calling https://graph.facebook.com/<name> and parse the result.
I had the same issue. I searched for a while and tried all the answers on here Open facebook page from android app (in facebook version > v11) and there Open Facebook page from Android app?, finally I figured out what the real problem is. There is nothing wrong in the code, however the weird behavior is about facebook app itself. If the facebook app is on the background, it just switches back to the foreground without navigating to the requested page. You just swipe it out from the background (kill instances) and try your code again.
Considering most of the users leave facebook app on the background, this is some kind of an issue needs to be fixed. Although I think it is about the facebook app itself, somehow it can be related with the device. I'm using Nexus 6 with v 6.0.1 and had no chance to test it on another android versioned device.
i using this for my apps its working just fine, kotlin
try {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/100674618166286"));
startActivity(intent);
} catch(e: Exception) {
startActivity( Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/iqtousd")));
}
Remember have a similar issue.
Try the mobile url :
instead of
String facebookUrl = "https://facebook.com/...";
use
String facebookUrl = "https://m.facebook.com/...";

Opening Facebook app with another ? With the "Twitter way"

My app needs to display a Facebook account. So I would like the Facebook application opens or if it is not available, the web browser. Currently I can open the Twitter app with this way and the system ask what to do (Twitter app or the browser).
Here is the code:
WebView myWebView = (WebView) findViewById(R.id.webViewProfile); // Create an instance of WebView and set it to the layout component created with id webview in main.xml
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("https://mobile.twitter.com/"+name); // Specify the URL to load when the application starts
myWebView.setInitialScale(1); // Set the initial zoom scale
myWebView.getSettings().setBuiltInZoomControls(true); // Initialize zoom controls for your WebView component
myWebView.getSettings().setUseWideViewPort(true); // Initializes double-tap zoom control
myWebView.getSettings().setUserAgentString("BumpMe");
This method is very good and I want to have the same result for Facebook. Currently I can open the Facebook app like that:
String uri = "facebook://facebook.com/info?user="+fbid;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);
But if the application is not installed it won't work...
How can I do it like for twitter ?
Thanks
The reason this works with Twitter is that the Twitter app handles https://mobile.twitter.com/* URL's in its intent filters; so when you issue an intent that has that URL, the Twitter app gets called. So the only way to do this with Facebook (or any other app) would be to use a URL that matches one of the patterns declared in its intent filters. Maybe http://m.facebook.com/... ?
Instead of offering the user the choice of app or browser, another way is to try to open the app, and if that fails to open the web page. Here is how I do that for Twitter:
long userID = tweet.getUser().getId();
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=" + userID));
startActivity(intent);
}catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/intent/user?user_id=" + userID)));
}

Categories

Resources