How to override a class within an Android library project? -
the following situation:
i have library project , project based on it. in library have 2 classes , b, whereby uses b. in project using library, have class b, should override class b library.
but every time class makes call, ends in class b library. how can tell android class b project should used instead of class b library?
that not work current layout. have use strategy pattern. in library define liba constructor takes object of type libb in constructor:
class liba{ libb b; public liba(libb b) this.b = b; } }
then can override libb in project , create liba class extends libb:
class projectb extends libb{ } liba = new liba(new projectb());
answer turbos question:
you want start project-activities library. move code creates intent
projects, because in project know type or name of activity
started.
the solution mentioned in comment (here) creates intent
in library project, guessing name of activity
should started. there nothing wrong it's not elegant solution. can start activities
follow special naming scheme. , because of cannot start arbitrary activities
visible in projects activities
other libraries, cannot change name of class.
to move intent
creation libraries can i.e. use strategy, template or factory-method pattern. see design patterns on wikipedia more (creational) patterns match library design.
a simple solution be:
create abstract libraryactivity
, extend projectactivities it. libraryactivity
defines abstract method returns intent
. implementation of abstract method done in projectactivities
.
abstract class libactivity{ private void dosomething(){ //library //and calls createintent() start project specific activity startactivity(this.createintent()); } protected abstract intent createintent(); } class projectactivity extends libactivity{ protected intent createintent(){ return new intent(projectactivity.this, anyactivityyouwant.class); } }
Comments
Post a Comment