2

Our app lets users login into other services. A user clicks on a Login button on a website (in some mobile browser) or on another app, which takes them to our app through a deep link. The user completes the login flow in our app. We'd then like to automatically redirect the user to where they started from, whether on a mobile browser or another app. I can't figure out how to do this in the general case.

My understanding is that redirecting to another app requires that app to have deep linking enabled. (Question #0: Is this true, or is there a way to do this even if the app doesn't have deep linking?)

Everyone I've asked seems to think this is only possible for apps and not mobile browsers, i.e. that there is no way to deep link into mobile browsers or otherwise redirect the user to them (except to the phone's default browser). But this doesn't make sense to me, since a mobile browser is just a kind of mobile app.

Also, there's at least an OS-level way to go back to another app or browser: both iOS and Android have such a back button, and it works with apps (even if they don't have deep linking) and mobile browsers. I don't know if this is accessible to us developers, but it suggests that the functionality might be possible.

Someone suggested to me that there was some way to redirect a user to an app by using something like com.appName, but I haven't been able to find anything online to support this.

To make this clearer, here's an example of a desired flow that captures the more subtle challenges:

Example 1. The user is using the Brave mobile browser, which is not their phone's default browser. 2. On some website, they click a Login button that takes them to our app, where they finish the login flow. 3. Our app takes them back to Brave, where they website the button was on updates to reflect the fact that they've completed the login process.

rob_weston
  • 83
  • 1
  • 6
  • Review [What is an Intent in Android?](https://stackoverflow.com/q/6578051/295004) and then explain what the user sees when `Our app takes them back to Brave, where they website the button was on updates to reflect the fact that they've completed the login process` and you don't control the third-party website. – Morrison Chang Jul 16 '19 at 21:47
  • @MorrisonChang The third-party website would update according to its own logic. Our app would tell the backend powering that website that the user was logged in, and the website would update accordingly. So what's tricky here is that I'm not trying to open a webpage in a specific browser. I'm trying take the user back to the page they were already on in a specific browser and let the page do what it will. – rob_weston Jul 17 '19 at 01:25

1 Answers1

1

You can URL in the mobile browser using Intent.

String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
//Or
//Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);

You can also open specific browsers using something along the following lines.

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setPackage("com.android.chrome");
try {
    startActivity(i);
} catch (ActivityNotFoundException e) {
    // Chrome is probably not installed
    // Try with the default browser
    i.setPackage(null);
    startActivity(i);
}

You could also allow the user to choose the browser.

String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
// Note the Chooser below. If no applications match, 
// Android displays a system message.So here there is no need for try-catch.
startActivity(Intent.createChooser(intent, "Browse with"));

Here's the link to the official docs for further reference.

Chrisvin Jem
  • 3,940
  • 1
  • 8
  • 24
  • Thanks @Chrisvin Jem for your answer. I don't think this is quite what I had in mind. I'm not trying to open a page in a mobile browser but rather take the user back to the browser, to a page they already had open. Is that possible with intents? – rob_weston Jul 17 '19 at 20:01
  • If you're only opening a single activity then you could theoretically use `finish` to close your app and 'return' back to the browser. – Chrisvin Jem Jul 17 '19 at 21:08
  • You should also be able to set the result data using `setResult` before `finish` ([Reference](https://developer.android.com/training/basics/intents/filters#ReturnResult)), but I've never actually worked with browser-app interactions, so I can't account for its correctness. – Chrisvin Jem Jul 17 '19 at 21:12