We specify current activity and start activity for an intent and we call it through the method startService(). So why do we need to mention that activity in manifest again?
Indexing (what can your app do?)
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.
Configuring (what is your main activity?)
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.
Time Saving (where can the system find your service?)
The manifest file is used by the system to know what kind of components do the application have. Without registering your Activities/Services/Receivers/Content Providers the system would have to scan and parse the whole apk every time someone wants to use a specific component to find it. This would be really slow, that's why there is the AndroidManifest.xml, which is a small file, and it can be parsed fast to find the required component.
Sources: Why do Activities/Services need to be explicitly added to the Android manifest? why acitivies have to be registered in manifest file
Each android application you build will include a file called
AndroidManifest.xml which is placed in the root of the project
hierarchy. So why is it important? Because it lets you define the
structure and metadata of your android application and its components.
http://simpledeveloper.com/android-application-manifest-file/
Related
I am adding a library into my project. It comes packaged as an AAR. But it has a BroadcastReceiver that listens to a BOOT_COMPLETED starts a service. I don't want this behavior in my app. I would like it to start whenever the app starts. (This part is already done through the AAR, I assume in case the user force stopped the app).
Is there a way to either modify their manifest; or specify in Gradle not to merge their manifest (in which i'd have to declare their activities in my own manifest, I think); or to override their manifest and have a android:enabled="false for the BroadcastReceiver"?
I also would want to do it in the manifest or Gradle, because if I do it programmatically and if they install the app but never open it, the library service would start automatically next time the user reboots their phone.
You could re-declare their BroadcastReceiver in your manifest and use android:enabled="false" followed by tools:node = "replace".
Also if you later want to enable it, you can by using the setComponentEnabledSetting() method inside of PackageManager.
You can just open the AAR file with WinRAR or something else and edit the manifest file yourself. AARs are simply packaged ZIP files, just like APKs.
Indeed, Android Studio could handle this more gracefully, but I don't know if this is possible yet
if I have a Service or an Activity that I want to be started only by another Activity in my app, do I need to declare it in the manifest? I.e., I always launch a secondary activity from the primary activity of my app that directly points the secondary activity's class (no intent filter resolution), is still necessary to declare the secondary activity in the manifest?
And what if I don't want anyone outside my app to be able to launch my secondary activity?
1.YES
2.YES
3.set exported false in the manifest.
Read here for more information
What is Manifest?
Manifest file for an android application is a resource file which contains all the details needed by the android system about the application. It is a key file that works as a bridge between the android developer and the android platform. It helps the developer to pass on functionality and requirements of our application to Android. This is an xml file which must be named as AndroidManifest.xml and placed at application root. Every Android app must have AndroidManifest.xml file. AndroidManifest.xml allows us to define,
The packages, API, libraries needed for the application.
Basic building blocks of application like activities, services and etc.
Details about permissions.
Set of classes needed before launch.
Do I need to declare it in the manifest?
Yes
Is still necessary to declare the secondary activity in the manifest?
Yes
What if I don't want anyone outside my app to be able to launch my secondary activity?
android:exported="false"
We must declare in AndroidManifresh according to https://developer.android.com/guide/topics/manifest/activity-element.html
Declares an activity (an Activity subclass) that implements part of
the application's visual user interface. All activities must be
represented by elements in the manifest file. Any that are
not declared there will not be seen by the system and will never be
run.
I want to create an Activity but not have to declare it in the manifest file. Is this possible? Everywhere I've seen it seems that every activity must be declared in the manifest, yet I notice that some activities, such as the built-in ChooserActivity, is not declared in my manifest file.
Short answer: yes, every Activity in your application must be declared in the manifest. As described in the Android docs, the purpose of the manifest (among other things) is:
It describes the components of the application — the activities, services, broadcast receivers, and content providers that the
application is composed of. It names the classes that implement each
of the components and publishes their capabilities (for example, which
Intent messages they can handle). These declarations let the Android
system know what the components are and under what conditions they can
be launched.)
http://developer.android.com/guide/topics/manifest/manifest-intro.html#ifs
Therefore any Activity class in your application must be defined in your Manifest. The same goes with Intents, Services etc. even if those components aren't accessible from outside of your application.
As for ChooserActivity and any other Activity which you didn't define in code, they will have their own definitions in another Manifest. If for whatever reason you decide to subclass an existing Activity outside of your application, then you will have to define it in your Manifest too.
I want to create an Activity but not have to declare it in the manifest file. Is this possible?
No, sorry.
I notice that some activities, such as the built-in ChooserActivity, is not declared in my manifest file
That activity is not part of your application. It is part of the core operating system.
we worked for exam with our friends. I tried to explain manifest file, talking about how to do things. But I see that I did know why to register activities to androidManifest.xml. Still do not know :). Does anyone have an idea?
The manifest file is used by the system to know what kind of components do the application have. Without registering your Activities/Services/Receivers/Content Providers the system would have to scan and parse the whole apk every time someone wants to use a specific component to find it. This would be really slow, that's why there is the AndroidManifest.xml, which is a small file, and it can be parsed fast to find the required component.
Manifest file describes the following features...
Version Number and Version Code:
This is useful, when you are uploading the application in Google play or go for upgrade the existing published apk.
2.minSdk and targetSdk:
mentioned your min and max version number your application supports.
3.application tag:
used to set the first activity or home activity when you launch an application from laucher.
4.activity tag:
the list of all activities in the application declared as child tag in application tag for the easier navigation to activity manager.
When we switch one activity to another activity, the activity manager checks whether this activity is declared in manifest file or not. If not found throws exception.
Uses: Developer can have look at all the activities at a glance (By Manifest file)
5.Filter tags: By intent filters in activity tag, User can open any kind of application activity.
Uses permission and Uses features:
All application resources are declared here.
example: Internet connectivity is needed for your application, Uses WIFI in your application.
I know you must declare all your Activities and Services in the AndroidManifest.xml file in order to make them accessible to the system (as said in the official docs) but if I have a Service or an Activity that I want to be started only by another Activity in my app, do I need to declare it in the manifest? I.e., I always launch a secondary activity from the primary activity of my app that directly points the secondary activity's class (no intent filter resolution), is still necessary to declare the secondary activity in the manifest? And what if I don't want anyone outside my app to be able to launch my secondary activity? I'm sorry if it is a stupid question, I just want to understand if it is a good practice (if possible) to omit activities and services from the manifest file when you want them to be started only by pointing their respective classes within the same app.
You must declare all of your activities and services (and everything else like BroadcastReceivers) in the AndroidManifest.xml file.
You won't be able to use them otherwise.
EDIT: As per CommonsWare comment, adding android:exported="false" to the AndroidManifest.xml's declaration of the activity would prevent your secondary activity from being launched outside of your application.