publicclassDemoActivityextendsAppCompatActivity{@OverrideprotectedvoidonCreate(@NullableBundlesavedInstanceState){super.onCreate(savedInstanceState);SomethingManager.getInstance().setsSomethingListener(newSomethingManager.SomethingListener(){@OverridepublicvoiddoSomething(){// do something}});}}classSomethingManager{staticSomethingManagersSomethingManager;publicstaticSomethingManagergetInstance(){if(sSomethingManager==null){sSomethingManager=newSomethingManager();}returnsSomethingManager;}privateSomethingListenersSomethingListener;publicvoidsetsSomethingListener(SomethingListenersSomethingListener){this.sSomethingListener=sSomethingListener;}voidrun(){sSomethingListener.doSomething();}interfaceSomethingListener{voiddoSomething();}
publicclassDemoActivityextendsAppCompatActivity{privateSomethingManager.SomethingListenermSomethingListener=newSomethingManager.SomethingListener(){@OverridepublicvoiddoSomething(){// do something}};@OverrideprotectedvoidonCreate(@NullableBundlesavedInstanceState){super.onCreate(savedInstanceState);SomethingManager.getInstance().setsSomethingListener(newWeakReference<>(mSomethingListener));}}classSomethingManager{staticSomethingManagersSomethingManager;publicstaticSomethingManagergetInstance(){if(sSomethingManager==null){sSomethingManager=newSomethingManager();}returnsSomethingManager;}privateWeakReference<SomethingListener>sSomethingListener;publicvoidsetsSomethingListener(WeakReference<SomethingListener>sSomethingListener){this.sSomethingListener=sSomethingListener;}voidrun(){SomethingListenersomethingListener=sSomethingListener.get();if(somethingListener!=null){somethingListener.doSomething();}}interfaceSomethingListener{voiddoSomething();}}