Android abstract activity in manifest

2019-04-29 12:27发布

问题:

For my application I am going to create a variety of abstract classes that extend the android.app.Activity and android.app.Service classes.

  • When I subclass my abstract classes how do I add them to the android manifest?
  • Do I need to add both the abstract class and my sub class to the manifest or just the subclass?
  • Do they need to be in the same package?

回答1:

You add the final subclasses to the manifest as regular Activities/Services; the abstract classes don't need to be there, as the manifest is only a lookup so the system knows which class to launch in response to an Intent

If by 'package' you mean Java package (e.g. com.mycompany.whatever), then no, just add the relevant import (or use the fully qualified name) when creating the subclass.

If by 'package' you mean APK, then yes, the abstract base should be in there with the regular code, as although it is possible to call between APKs, you this relies on classes you can instantiate. You can split the abstract classes out into an Android library project, if they're going to get re-used - Android library projects are essentially shared source, rather than traditional Java jars.

Let me know if you need more details on any of this, as this is quite a broad question, and I'd like to keep the answer size managable



回答2:

You only need to include Activity classes that you are going to instantiate with an Intent. If your abstract class only exists to subclass other Activities, you shouldn't need to include it in the Manifest.



回答3:

You will add the subclasses to the manifest just like any other Activity/Service/BroadcastReciever. You do not have to add the abstract classes to the mainifest. Subclasses do not have to be in the same package as their parent if you import the parent's package.