Chrome Custom Tabs - Intent not being triggered (Android)
<p>I am working on implementing PayPal into my current Android app, and I have been advised to use Chrome Custom Tabs, but I cannot seem to get the Intent to be triggered. </p>

<p>I believe I have set the Intent up correctly in the AndroidManifest.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rhiannon.chromecustomtabs"&gt;

    &lt;application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"&gt;
        &lt;activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

        &lt;activity android:name=".CompletePayPalPaymentActivity"&gt;
            &lt;intent-filter android:autoVerify="true"&gt;
            &lt;action android:name="android.intent.action.VIEW" /&gt;
            &lt;category android:name="android.intent.category.DEFAULT" /&gt;
            &lt;category android:name="android.intent.category.BROWSABLE" /&gt;
            &lt;data
                android:scheme="Test"
                android:host="com.Test.TestApp.PayPalReturn"/&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;
&lt;/manifest&gt;
</code></pre>

<p>I am able to successfully launch the Google Chrome Tabs in my Fragment class (Where I have got 2 buttons:</p>

<ul>
<li>A button that will launch www.google.co.uk in a Chrome Custom Tab</li>
<li>A button that launches my custom webpage (hosted on localhost) that will throw the redirects when the user clicks a button/link</li>
</ul>

<p>Please see below the code for this Fragment:</p>

<pre><code>public class MainFragment extends Fragment implements CustomTabsSceneHelper.ConnectionCallback {

    public enum CustomTabsAction {
        GOOGLE ("Open Google", "http://google.co.uk"),
        REDIRECT ("Open Redirect", "http://{mylocalhost_ip}/~Rhiannon/example1/test.html");

        private final String mActionDescription;
        private final String mActionUrl;

        CustomTabsAction (
                final String actionDescription,
                final String actionUrl) {

            mActionDescription = actionDescription;
            mActionUrl = actionUrl;
        }

        public String actionDescription () {
            return mActionDescription;
        }

        public String actionUrl () {
            return mActionUrl;
        }
    }

    private CustomTabsSceneHelper mCustomTabsSceneHelper;
    private CustomTabsAction mCurrentAction;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mCustomTabsSceneHelper = new CustomTabsSceneHelper();
        mCustomTabsSceneHelper.setConnectionCallback(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_main, container, false);
        Button buttonGoogle = (Button) view.findViewById(R.id.button_google);
        buttonGoogle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                open(CustomTabsAction.GOOGLE);
            }
        });

        Button buttonRedirect = (Button) view.findViewById(R.id.button_redirect);
        buttonRedirect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                open(CustomTabsAction.REDIRECT);
            }
        });

        return view;
    }

    private void open(CustomTabsAction action) {
        final Activity scene = getActivity ();
        mCurrentAction = action;

        CustomTabsSceneHelper.openCustomTab(
                scene,
                getCustomTabIntent(scene, mCustomTabsSceneHelper.occupySession()).build(),
                Uri.parse(action.actionUrl())
        );
    }

    public static CustomTabsIntent.Builder getCustomTabIntent(
            @NonNull Context context,
            @Nullable CustomTabsSession session) {

        // Construct our intent via builder
        final CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder (session);
        // Toolbar color
        intentBuilder.setToolbarColor(Color.GREEN);
        // Show title
        intentBuilder.setShowTitle(true);
        // Allow hiding for toolbar
        intentBuilder.enableUrlBarHiding();

        return intentBuilder;
    }

    @Override
    public void onCustomTabsConnected() {
        Log.i("MainFragment", "Custom Tabs &gt; CONNECTED");
        if(mCurrentAction != null){
            mCustomTabsSceneHelper.mayLaunchUrl(Uri.parse(mCurrentAction.actionUrl()), null, null);
        }
    }

    @Override
    public void onCustomTabsDisconnected() {
        Log.i("MainFragment", "Custom Tabs &gt; DISCONNECTED");
    }

    @Override
    public void onStart() {
        super.onStart();
        mCustomTabsSceneHelper.bindCustomTabsService(getActivity());
    }

    @Override
    public void onStop() {
        mCustomTabsSceneHelper.unbindCustomTabsService(getActivity());
        super.onStop();
    }

    @Override
    public void onDestroy() {
        mCustomTabsSceneHelper.setConnectionCallback(null);
        super.onDestroy();
    }

}
</code></pre>

<p>I then have my custom webpage:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;PayPal Test Redirect&lt;/title&gt;
    &lt;script type="text/javascript"&gt;
        function RedirectSuccess(){
            window.location="Test://com.Test.TestApp.PayPalReturn://return";
        }

        function RedirectCancel(){
            window.location="Test://com.Test.TestApp.PayPalReturn://cancel";
        }
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;a href="Test://com.Test.TestApp.PayPalReturn://return"&gt;Success&lt;/a&gt;
&lt;a href="Test://com.Test.TestApp.PayPalReturn://cancel"&gt;Cancel&lt;/a&gt;

&lt;input type="button" onclick="RedirectSuccess();" name="ok" value="Success"  /&gt;
&lt;input type="button" onclick="RedirectCancel();" name="ok" value="Cancel"  /&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>I would expect that when the user clicks on one of the links/buttons on the webpage in the Chrome Custom Tab that the user would be redirected back to the application, but I cannot seem to get this functionality to work.</p>

<pre><code>public class CompletePayPalPaymentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complete_paypal);

        List&lt;String&gt; params = getIntent().getData().getPathSegments();
        if(params.get(0).equals("return")){
            Log.i("CompletePP", "PayPal Payment Success");
        }else{
            Log.i("CompletePP", "PayPal Payment Cancelled");
        }
    }
} 
</code></pre>

<p>Any help at all, would be greatly appreciated! </p>