tags:

views:

28

answers:

1

Hi,

I have an android app. I added an intent filter to one of my activities, so that if a url is clicked in the browser app, my activity can be launched. Looks like this:

<data android:host="www.mysite.com" android:scheme="http"></data>

this works well. The problem is that every time my activity is launched from the browser, a new instance of the activity is created, inside the browser app's task, instead of recycling any existing instance that may be in the system already.

This is a problem for me because this activity uses a singleton. If I allow more than one instance of this activity to exist, I can get into some weird situations where the two instances are in conflict when they try to share the singleton. I checked and can see that although the activity instances may be in separate tasks, they do share the same singleton instance.

An ideal solution for me would be if I could somehow bring a pre-existing instance of my application to the foreground, and launch or resume the target activity within whatever pre-existing instance of my app happens to be running.

So I tried this:

  1. Register above filter to point to dummy activity which is just a catcher.
  2. Dummy activity creates a broadcast intent, and tries to broadcast 'create me' message to system.
  3. Real target activity is set to listen for this broadcast message. Hopefully if there is already an instance of the activity in the system, it will come to the foreground. If no instance yet, that's ok, allow creation in the browser task.

Not sure if this makes sense. My basic goal is to limit the activity to one instance in the system. The app is just social media app which has a login state that needs to be preserved. The singleton mentioned above preserves that login state, so I want to have only one around in the system, instead of allowing multiple login instances running around which would be a headache for the user.

Thanks

A: 

You can set the activity launch mode to singleTop to achieve this.

android:launchMode An instruction on how the activity should be launched. There are four modes that work in conjunction with activity flags (FLAG_ACTIVITY_* constants) in Intent objects to determine what should happen when the activity is called upon to handle an intent. They are: "standard" "singleTop" "singleTask" "singleInstance" The default mode is "standard".

http://developer.android.com/intl/zh-TW/guide/topics/manifest/activity-element.html#lmode

sadboy
Hi sadboy, I have set launchMode to singleTop, but still no effect. I can print the mem address of the activity, and see that each time the browser starts a new instance, it is a different mem address, instead of just resuming the already-running instance.
Thats weird. Are you putting it in the Application manifest where you declare the activity? And I'm guessing you tried the other launch modes like singleinstance too?
sadboy