and can't make updateUI() static.
True! You want to minimize the usage of static
stuff in any of your activities.
I don't know how good you are at Java, but I use the so called observer-pattern a lot for this kind of problems. You might need to convert it to Kotlin
.
If the MainActivity
is running, it registers to receive events from the FBManager
.
I use the following code from Telegram quite a lot:
public class NotificationCenter {
private static int totalEvents = 1;
public static final int updateActivity = totalEvents++;
private final SparseArray<ArrayList<Object>> observers = new SparseArray<>();
private final SparseArray<ArrayList<Object>> removeAfterBroadcast = new SparseArray<>();
private final SparseArray<ArrayList<Object>> addAfterBroadcast = new SparseArray<>();
private int broadcasting = 0;
public interface NotificationCenterDelegate {
void didReceivedNotification(int id, Object... args);
}
private static volatile NotificationCenter Instance = null;
public static NotificationCenter getInstance() {
NotificationCenter localInstance = Instance;
if (localInstance == null) {
synchronized (NotificationCenter.class) {
localInstance = Instance;
if (localInstance == null) {
Instance = localInstance = new NotificationCenter();
}
}
}
return localInstance;
}
public void postNotificationName(final int id, final Object... args) {
if (Thread.currentThread() == ApplicationLoader.applicationHandler.getLooper().getThread()) {
postNotificationNameInternal(id, args);
} else {
ApplicationLoader.runOnUIThread(new Runnable() {
@Override
public void run() {
postNotificationNameInternal(id, args);
}
});
}
}
private void postNotificationNameInternal(int id, Object... args) {
broadcasting++;
ArrayList<Object> objects = observers.get(id);
if (objects != null && !objects.isEmpty()) {
for (int a = 0; a < objects.size(); a++) {
Object obj = objects.get(a);
((NotificationCenterDelegate) obj).didReceivedNotification(id, args);
}
}
broadcasting--;
if (broadcasting == 0) {
if (removeAfterBroadcast.size() != 0) {
for (int a = 0; a < removeAfterBroadcast.size(); a++) {
int key = removeAfterBroadcast.keyAt(a);
ArrayList<Object> arrayList = removeAfterBroadcast.get(key);
for (int b = 0; b < arrayList.size(); b++) {
removeObserver(arrayList.get(b), key);
}
}
removeAfterBroadcast.clear();
}
if (addAfterBroadcast.size() != 0) {
for (int a = 0; a < addAfterBroadcast.size(); a++) {
int key = addAfterBroadcast.keyAt(a);
ArrayList<Object> arrayList = addAfterBroadcast.get(key);
for (int b = 0; b < arrayList.size(); b++) {
addObserver(arrayList.get(b), key);
}
}
addAfterBroadcast.clear();
}
}
}
public void addObserver(Object observer, int id) {
if (broadcasting != 0) {
ArrayList<Object> arrayList = addAfterBroadcast.get(id);
if (arrayList == null) {
arrayList = new ArrayList<>();
addAfterBroadcast.put(id, arrayList);
}
arrayList.add(observer);
return;
}
ArrayList<Object> objects = observers.get(id);
if (objects == null) {
observers.put(id, (objects = new ArrayList<>()));
}
if (objects.contains(observer)) {
return;
}
objects.add(observer);
}
public void removeObserver(Object observer, int id) {
if (broadcasting != 0) {
ArrayList<Object> arrayList = removeAfterBroadcast.get(id);
if (arrayList == null) {
arrayList = new ArrayList<>();
removeAfterBroadcast.put(id, arrayList);
}
arrayList.add(observer);
return;
}
ArrayList<Object> objects = observers.get(id);
if (objects != null) {
objects.remove(observer);
}
}
}
Then make your MainActivity
look like this:
public class MainActivity implements NotificationCenter.NotificationCenterDelegate {
@Override
public void onPause() {
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.updateActivity);
super.onPause();
}
@Override
public void onResume() {
NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateActivity);
super.onResume();
}
@Override
public void didReceivedNotification(int id, Object... args) {
if (id == NotificationCenter.updateActivity) {
UpdateUI();
}
}
}
Then in your FBManager
:
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateActivity, optionalData);
See the other code from this implementation here: https://github.com/gi097/MyWindesheim/tree/master/app/src/main/java/com/giovanniterlingen/windesheim