Jeka
Jeka

Reputation: 1690

How to re-open (or resume) previously launched activity from a url link (deep link)?

I'm using deep linking to open my app this is the intent filter:

        <intent-filter>
            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:host="myapp.delivery" />
            <data android:pathPattern="/.*" />

            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>

Our use case is:

  1. user open apps, browses some restaurant
  2. user clicks restaurant (openning MenuActivity an activity with the restaurants menu)
  3. user click on a menu category for example Pizzas or Desserts or Soups, which opens the CategoryActivity.
  4. user clicks on a Dish to add to shopping cart I need to send an email with a url to the user.

My problem:

(Following step 4) User clicks url on email app (say gmail or inbox app) which prompts user with dialog to open my app or browser like this:

enter image description here

When user clicks to open with my app what happens is that the GotEmailActivity is opened on top of gmail app, I need it to open on top of previously opened instance of my app.

As you can see here 2 instances of my app are open (the first instance of app and the second instance is open on top of gmail),:

enter image description here

How to open clicked link in my previously launched app not on top of gmail/inbox app?

Upvotes: 5

Views: 5717

Answers (2)

Shaho Bashoki
Shaho Bashoki

Reputation: 21

set this code in AndroidManifest.xml

android:launchMode="singleTask"

in tag activity

<activity
        android:name=".ui.live.liveinfo.LiveInfoActivity"
        android:clearTaskOnLaunch="true"
        android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|keyboardHidden"
        android:launchMode="singleTask"
        android:parentActivityName=".ui.main.MainActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />

            <data
                android:host="playpod.ir"
                android:pathPrefix="/lives/live"
                android:scheme="https" />
        </intent-filter>
    </activity>

Upvotes: 2

Jeka
Jeka

Reputation: 1690

Answer I found to work

So I created EntryActivity which is launched on top of gmail/inbox app:

public class EntryActivity extends Activity {

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

        Uri uriParams = getIntent().getData();

        Log.e("EntryActivity", uriParams.getHost() );
        Log.e("EntryActivity", uriParams.getQueryParameter("uid") + " " + uriParams.getQueryParameter("type") + " " + uriParams.getQueryParameter("token") );


        Intent startCategory = new Intent(this, GotEmailActivity.class);
        startCategory.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startCategory);
        this.finish();
    }

}

Now what is happening is that EntryActivity is opened ontop of Gmail app but it closes inmediatle but first launches GotEmailActivity which is already opened so attribute launchMode Singletop prevents a new instance of such activity.

Then when my app is opened at GotEmailActivity I send email to user with link to open app and GotEmailActivity has attribute android:launchMode="singleTop" in AndroidManifest so only 1 instance of it is opened:

    <!-- 
        Important: notice android:launchMode="singleTop"
        which seeks if an instance of this activity is already opened and
        resumes already opened instance, if not it opens new instance.
     -->
    <activity
        android:name=".presenters.register.email.GotEmailActivity"
        android:label="@string/title_activity_got_email"
        android:launchMode="singleTop" 
        android:theme="@android:style/Theme.Translucent.NoTitleBar" >

Merit of answer goes to: https://stackoverflow.com/a/34499615/5297353

Upvotes: 12

Related Questions