The following is my situation:
I have a library project and a project based on it. Now in the library I have two classes A and B, whereby A uses B. In the project using the library, I have another class B, which should override the class B from the library.
But every time class A makes a call, it ends up in the class B from the library.
How can I tell Android that class B from my project should be used INSTEAD of class B from the library?
That does not work with the current layout. You have to use the strategy pattern. In your library define LibA with a constructor that takes a object of type LibB in the constructor:
Then you can override LibB in your project and create LibA with the class that extends LibB:
Answer to Turbos question:
You want to start Project-Activities from your Library. So then move the code that creates the
Intentinto your Projects, because only in your project you know the type or name of theActivityto be started.The solution you mentioned in your comment (here) creates the
Intentin the library project, by guessing the name of theActivitythat should be started. There is nothing really wrong with that but it’s not an elegant solution. You can only startActivitiesthat follow that special naming scheme. And because of that you cannot start arbitraryActivitiesthat are visible in your projects likeActivitiesfrom other libraries, where you cannot change the name of the class.To move the
Intentcreation into your libraries you can i.e. use the strategy, template or factory-method pattern. See Design Patterns on Wikipedia for even more (creational) patterns that match your library design.A simple solution would be:
Create an abstract
LibraryActivityand extend your ProjectActivities from it. TheLibraryActivitydefines an abstract method that returns theIntent. The implementation of that abstract method is done in yourProjectActivities.