Search code examples
androidandroid-activityoverridingextend

Properly extending/overriding a library Activity, effectively replacing it in a new project?


I have Library Project A, and child project B. The only difference in B is that a single activity needs to have additional code (for example, for ads)

I know I can just copy A.Activity to B and add code, but if I change code in A, it will not be in B, so this is not an option.

How do I extend A.Activity into B.Activity and add a little code, while keeping it in the place in the manifest (So pushing a button in B.Activity 1, I go to B.Activity 2(which extends from A.Activity)

Detailed scenario:

In project A, when clicking a button in FirstActivity, I go to SecondActivity. I want to override SecondActivity, when in project B, I click on FirstActivity (which is the same one as from A) I go to SecondActivity, which is overridden/extended.


Solution

  • I've found extending activity classes from library projects to be cumbersome and to eventually lead to problems in larger projects. Maybe I'm doing it wrong!

    Instead I put all the required functionality into the library project and use a configuration resource (boolean flag for example) to determine whether to enable this functionality or not.

    The default configuration is in the library project and then I just override it in xml in the child project.