Is there a way to have a Linkify
'd link call a method in my Activity
or am I only allowed to use Linkify
to create links to other activities / apps etc.?
4 Answers
YOu could also just abandon using linkify and just use the regex of linkify to go and attach clickable spans to the places you want. Those clickable spans can call into your code.
Here's an example: Android: Launch activity from clickable text

- 1
- 1

- 10,164
- 2
- 47
- 64
-
interesting, this looks very promising, i will try it later today and get back... – Ben Mar 22 '11 at 16:07
It sounds like you've got several pieces of this worked out. The missing piece seems to be having an existing Activity which is in the foreground receive the Intent. You can do that by declaring the Activity to be singleTop in the Manifest (android:launchMode="singleTop"
).
So the workflow looks like:
1) Linkify the text by addLinks with your regex, Linkify.Matcher, and Linkify.TransformFilter
2) Set up a receiver to catch the Intent launched by the clicked link.
3) In the onReceive of the associated BroadcastReceiver, set up an Intent to encapsulate the fact that your link has been clicked. Then call startActivity on the Activity (which is already in the foreground), passing the Intent.
4) In the onResume callback on the Activity, check the Intent to see if the function should be called.

- 11,622
- 4
- 40
- 39
-
yes but doesn't singleTop cause only one instance of that activity to be in the stack at a time? I don't want that behavior. this activity is a generic "view user profile" activity that can have multiple instances in the nav stack... – Ben Mar 23 '11 at 15:54
-
No, singleTop allows multiple instances, but if the target task has an instance of the singleTop Activity on top of the Activity stack, it uses that instance. http://developer.android.com/guide/topics/manifest/activity-element.html#lmode – Brian Cooley Mar 23 '11 at 16:16
-
So assuming that the Linkify'd text is on the Activity itself, that Activity would have to be on top of the stack for the link to be clicked as far as I can tell, and it should work. – Brian Cooley Mar 23 '11 at 16:18
-
hmm ok i'll give that a go. i think if that works, that'd be the winner... now if i can just wrap up this iphone code i'm working on so i can get back to android :) – Ben Mar 23 '11 at 17:16
You can set a receiver which will listen for a particular scheme and when you click on the link call some url starting with that scheme.
Check this answer for more details.
-
yes but doesn't that start a new activity? I want the linkify'd code to call a method in the same activity in which it was created without actually starting a new one. Lets say for example that when they click on a link, i want to show a popup w/ some menu options for example. I dont want it to create a new activity, i want it to run right from the existing one... – Ben Mar 20 '11 at 15:22
-
Yes, it will start new activity and that activity will broadcast the same intent (and call finish for self) which will be handled in your app. – Karan Mar 21 '11 at 13:50
-
thanks Karan, but unfortunately that's not gonna work for me, I need it to call a method within that running activity, without starting any new activity. – Ben Mar 21 '11 at 22:47
Nope. Linkify comes with:
EMAIL_ADDRESSES
MAP_ADDRESSES
PHONE_NUMBERS
WEB_URLS
You can try extending it. Here's the src code.

- 91,829
- 44
- 175
- 230
-
1that's not totally true, you can create links to custom urls and match on things other than what you mentioned using Linkify.addLinks(holder.messageText, myRegexPattern, "content://com.my.app/people/?url=",null, new MyLinkTransform(msg.getUrl())); – Ben Jul 12 '10 at 15:58
-
@Ben: You are right. What I try to say is that it brings that by default. I will modify my answer. – Macarse Jul 12 '10 at 16:15
-
1I wish they had an api similar to the webview (webview.setJavascriptInterface()) where i could essentially tack-on my own custom java code when they click on the link... – Ben Jul 12 '10 at 16:49