Naim Jamalludin
Naim Jamalludin

Reputation: 195

How to resume to the launcher app from the deep link app on Android?

So I have two apps as described below:

Merchant App

  1. User choose their desired items.
  2. User clicks on the checkout button. This will open the Payment App via deep link

Payment App

  1. Payment App picks up the information passed by Merchant App and display it.
  2. User clicks confirm and user will be asked to swipe their credit card to complete payment.
  3. Once payment is completed, the Payment App shall send the payment status back to the Merchant App thus close itself.

Issue

How do I resume back to the Merchant App from the Payment App after it is being deep inside (few activities in) in Payment App? Because at the moment, every time I try to finish the activity, it will go to the Main Activity of the Payment App.

Illustration of the issue

Merchant App (Checkout Activity) -> Payment App (Confirm Activity) -> Payment App (Payment Activity) -> Payment App (Main Activity)

Illustration of what I'm trying to achieve

Merchant App (Checkout Activity) -> Payment App (Confirm Activity) -> Payment App (Payment Activity) -> Merchant App (Checkout Activity) -> Merchant App (Status Activity)

Code

Merchant App - Checkout Activity

Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);

//Verify if receiver app XXX this screen path
PackageManager packageManager = this.context.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
if (isIntentSafe) {
     ((Activity) context).startActivityForResult(intent, 2);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 2) {
        String message = data.getStringExtra("MESSAGE");
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}

Payment App - Payment Activity

Intent intent = new Intent();
intent.putExtra("MESSAGE", "test");
setResult(2, intent);
finish();

Please advice on where am i making mistake here? Or it is not a good practice to do such thing?

Thank you in advance

Upvotes: 1

Views: 771

Answers (1)

Tam Huynh
Tam Huynh

Reputation: 2477

My first thought is:

  • Use a temporary activity call Payment Confirm Activity in Payment App to clear all previous activities of Payment App and start a deep link to Merchant App
  • You should use another Checkout Activity for handling the result from Payment App, let's say it's Checkout Confirm Activity

So the flow will be:

Merchant App (Checkout Activity) -> Payment App (Confirm Activity) -> Payment App (Payment Activity) -> Payment App (Payment Confirm Activity) -> Merchant App (Checkout Confirm Activity) -> Merchant App (Status Activity)

Steps:

1/ PaymentActivity will start PaymentConfirmActivity to clear all the previous activities using this code:

Intent intent = new Intent(getApplicationContext(), PaymentConfirmActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

2/ PaymentConfirmActivity is generally blank, it starts a deep link to Merchant App to notify the result, including all input data

3/ This CheckoutConfirmActivity will handle the result from the Payment App by the deep link above (configured format in AndroidManifest). Get the input, get the result and show a confirmation page

Edited: You can reuse Merchant App CheckoutActivity (no need for CheckoutConfirmActivity) by applying singleTop launch mode. Found a blog post here. CheckoutActivity can receive the result in onNewIntent if it's still the topmost activity in MerchantApp

4/ Move to StatusActivity like normal

Upvotes: 1

Related Questions