mercredi 18 novembre 2015

Implementing a universal Observer Pattern/Listener for the app

In my android app there's a lot of updates that need to be done after the lifecycle has been started and passed the initial states. Right now I was working on adding an option to allow the user to disable receiving push notification , and I had to create a custom "listener" so it would reach a method that's inside an onCreate callback, and I've noticed it is not the first time I'm doing this. So I was wondering, is there a way to make some "abstract" interface, instead of implementing a new listener for each class (there are dozens of them...).

Just for the same of making myself more clear , here's a pseudo-code of what I mean:

interface X:
{
   void onDoneFragment1();
   void onDoneFragment2();
   void onDoneFragment3();
...
}

and then

fragment 1:
..
x.setOnListenFragment1( new OnDoneFragment1(){
@Override
public onDoneFragment1(){
......
..

same for fragment 2 and others..

 fragment 2:
    ..
    x.setOnListenFragment2( new OnDoneFragment2(){
    @Override
    public onDoneFragment2(){
    ......
    ..

Aucun commentaire:

Enregistrer un commentaire