Why do Activities/Services need to be explicitly a

2019-05-04 12:06发布

问题:

In Android development, you have to add all Activities and/or Services to the manifest file. I understand how to do this, but I don't understand why I need to do this. It seems like an unnecessary hoop to jump through.

The Android documentation helps, but I'm still not clear on "why" (http://developer.android.com/guide/topics/manifest/manifest-intro.html).

回答1:

I can answer at least part of the why for Activities.

The manifest is also where you declare your IntentFilter which is how the system understands what your application does. i.e. should your activity be an illegible choice when the user is trying to take a picture? choose a file? share a piece of text? In addition to that the IntentFilter also tells the Launcher application that you would like to have your activity included in the Applications drawer.

There are also several configuration options that you can set on Activities which have to be done in the manifest i.e. SingleTop. Without the declaration in the manifest there would be no place to declare these configurations.



回答2:

The rule is: only those Activities listed in the Manifest can be invoked. Some of those listed can be invoked not only by your application but by some other application(s). So Android scans the manifest and determines which Activities can be invoked. I think they just follow the simplest possible logic here. Any 'defaults' such as 'it's not needed to declare an Activity in the Manifest if..' would be unnecessary complications. They should not care for programmer's convenience instead they care for clarity (I think).