Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Activities/Services need to be explicitly added to the Android manifest?

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).

like image 811
loeschg Avatar asked Jan 14 '23 18:01

loeschg


2 Answers

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.

like image 137
FoamyGuy Avatar answered Jan 31 '23 18:01

FoamyGuy


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).

like image 40
Alexander Kulyakhtin Avatar answered Jan 31 '23 18:01

Alexander Kulyakhtin