Android - Launch multiple methods in others Activities

KevinB :

I'm really new to Android.

I have a fitness app where the user can change it's gym. There are several activities where the content depends on the user's gym. One activity will show the workouts of the gym, another the athletes of the gym, etc...

What I would like, is to update all of these activities when the user change it's gym.

I know how to do that in iOS, I just need to add an observer NotificationCenter.default.addObserver(), but I really don't know how to do that for android.

Is it possible? If yes, how?

Tomas Jablonskis :

First of all, according to Google your app should contain only one entry point, in other words one Activity, and have Fragments to represent contents of application.

Second of all, for solution to your problem you could use SharedPreferences. It has onSharedPreferencesChangeListener(); which could be used to listen for updates of information (e.g. GYM name, your custom user permissions, user role, etc.). When something changes in SharedPreferences you notify all dependent objects of application to change their information accordingly by using Observer pattern (P.S. can use RxJava for that). You could also implement SharedPreferences change listener in all the Fragments and updated their data there.

If, for whatever reason, you want to stick with using multiple Activities in your application, you could still use SharedPreferences, but instead of listening to changes, you just read preference values in Activity onCreate(); method and create content accordingly (change item visibility, color, etc.).


That's one of the approaches. Hope this puts you in the right direction. Good luck :)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=80563&siteId=1