Android + CustomView: Share the state of custom views while waiting for a network response

38 views Asked by At

I have the next escenario:

  • A activity has 2 fragments.
  • Each fragment has a custom view (the same but different instances).
  • One fragment algo invoke a DialogFragment to emulate the view increase in size to full screen, so it's has another of the same custom views.

When you click any of the custom views, it makes a network request and wait (with a spinner) until the response is done. My problem is that I want to "share" the state of the views, so when you click any of the custom views, you will see the spinner in the three views and all of them will wait for the response, and when the response arrives all of them will remove each spinner and let the custom views accesible again.

1

There are 1 answers

0
Birendra Singh On

You can maintain a static list of all the custom view instances (better week references to ensure no memory leak because of this list) in custom view class with a static state variable. You can add items in the list from constructor of custom view class. Now whenever state changes do whatever you want with all views as they are in list. If you use week reference don't forget null check.