Reputation: 234
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.
Upvotes: 3
Views: 1393
Reputation: 5954
In your child/non-lib project create a new activity (lets call it the child activity) which extends from the activity in the lib project (the parent). Then override the method(s) you want to modify.
In the manifest for the child project you will need to add the child activity and (i presume) make it the launch activity by giving it the appropriate intent filter.
If it is not the launch activity then you probably also have the problem of the intent in library project trying to launch the standard (non-overridden) version of the Activity. I have overcome this before using a sort of 'dynamic' intent system whereby instead of hard coding the class type for the intent, I get it from the Application object which returns a type.
e.g
In Library Project:
public class MainActivity extends Activity {
private void launchOverideableActivity(){
MyApp app = (MyApp) getApplication();
Intent i = new Intent(this, app.getIntentClassType());
startActivity(i);
}}
And override the application object so you can supply this dynamic class type:
public class MyApp extends Application {
public Class getIntentClassType(){
return MainActivity.class;
}}
Then in your child project, extend your activity and do your custom work. But also override the application object from the library project and override your method but return your overridden activity class type:
public class OveriddenApp extends MyApp {
@Override
public Class getIntentClassType() {
return MyOveriddenActivity.class;
}}
Extending activities from library project like this is a bit clunky but in the long run it does allow clearer code separation so this little bit of coding overhead is worth it in my opinion.
I'd love to know if there is a more elegant way to do this...
Upvotes: 2
Reputation: 23269
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.
Upvotes: 1