开发者

Android taskAffinity problem with specifying activity

开发者 https://www.devze.com 2023-03-14 00:58 出处:网络
I am dealing with a strange issue here. Basically, I have one application (made by another developer) that has an activity that specifies its taskAffinity to \"\" and its launchMode to \"singleTask\"

I am dealing with a strange issue here.

Basically, I have one application (made by another developer) that has an activity that specifies its taskAffinity to "" and its launchMode to "singleTask".

When the phone receives a notification, I launch an activity in my application with FLAG_ACTIVITY_NEW_TASK flag.

I would like my activity to have a taskAffinity for that other application if the task exists.

When I do dumpsys activity I see that the other developers application TaskRecord is:

aj.streetPhone开发者_如何学编程.app/.ViewActivity

However, in my manifest on my activity I specify aj.streetphone.app it does not seem to add my activity to that applications task. I have tried another package name and it seems to be working. The problem perhaps has to do with the fact that the task name includes the activity, not just the package name (the default).

I am basically having a problem to use the '/' because the manifest does not accept it.

android:taskAffinity="aj.streetPhone.app/.ViewActivity" <--- does not work!

I have tried escaping the '/', added by unicode, everything. Please help!

Any ideas?


Task affinities (and permissions, and processes, and shared user IDs, and actions, etc etc) are NOT qualified by the app package name. They are a single string, much must be globally unique. Just adding a ".foo" does not make it unique because another app could have a package name that is a shorter or longer version of your own and also come up with the same name. So for affinities and processes, you can use a ":" prefix in the name to append it to your own package name and create a name that is guaranteed unique for your package.

So you have to use a unique string for your task affinity.


The / is indeed invalid.

Unless it's been abbreviated in some way (a quick look at dumpsys activity for my own app seems to show that it isn't) your activity's full name is aj.streetPhone.app.ViewActivity. However, if you want to prepend your package name to the taskAffinity you can simply use a colon, e.g. :myAffinity. That said, the taskAffinity is not meant to be the activity's name!

As I understand it, taskAffinity is meant to control which "task" an activity belongs to. While I can't comment on why you'd try to stick an activity to another app's task, you may want to clear the affinity to indicate the activity can be in any task; although I haven't tried it I imagine this would result in, for example, the back button leading back to whatever activity the user was on before rather than leading back to whatever activity was last active in your app (your task).

More info (which, FYI, is where chemalarrea was quoting from without giving credit): https://groups.google.com/forum/?fromgroups=#!topic/android-developers/wKwnV1k_96A

Documentation: https://developer.android.com/guide/topics/manifest/activity-element.html#aff

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号