I need to set up a simple event listener to refresh a ListView once in a while. The problem is I don\'t know how could I generate an event.
I know that
You can use android life cycle for that.
Create a signal interface, aka your event
interface NewsUpdateSignal{
void newsUpdateHandler(Mydata data);
}
Than register to it inside your activity or anywhere else you want, there could be many listeners to same Signal.
class MyActivity extends Activity implements NewsUpdateSignal{
Signal newsUpdateSignal = SignalsBag.inject(NewsUpateSignal.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
newsUpdateSignal.addListener(this);
}
@Override
public void newsUpdateHandler(final Mydata data){
//Do something here
}
}
And dispatch the signal when you need, from where ever you need.
Class A{
Signal newsUpdateSignal = SignalsBag.inject(NewsUpateSignal.class);
void execute(){
// obtain the data somehow, and dispatch it, the callback will be invoked on the same thread
newsUpdateSignal.dispatcher.newsUpdateHandler(data);
}
}
Disclaimer: I am the author of android life cycle.