Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>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.</p> <p>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.</p> <p>e.g</p> <p>In Library Project:</p> <pre><code>public class MainActivity extends Activity { private void launchOverideableActivity(){ MyApp app = (MyApp) getApplication(); Intent i = new Intent(this, app.getIntentClassType()); startActivity(i); }} </code></pre> <p>And override the application object so you can supply this dynamic class type:</p> <pre><code>public class MyApp extends Application { public Class getIntentClassType(){ return MainActivity.class; }} </code></pre> <p>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:</p> <pre><code>public class OveriddenApp extends MyApp { @Override public Class getIntentClassType() { return MyOveriddenActivity.class; }} </code></pre> <p>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.</p> <p>I'd love to know if there is a more elegant way to do this...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload