0

I have a Config class which receives the settings from server. These settings are then applied in an activity method named ApplySettings(). I called this ApplySettings() function onCreate function. first time it worked fine but I could not detect setting change in Activity class. I want to call this ApplySettings() method from Config class as soon as settings change there. How can I do this? Thanks

Config.Java //Singleton class public class Config { public String name = ""; public void getSettingsFromServer() { name = "";//name received from server } } MyActivity.java public class SettingsSystem extends AppCompatActivity { private Config config = Config.getInstance(); protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings_system); ApplySettings(); } public void ApplySettings() { //settings are applied here } } 

Both classes are in separate files.

2 Answers 2

1

You would want to store the instance of MyActivity inside your config class.

Add a method in Config.java:

public class Config { public String name = ""; private SettingsSystem activity; public void getSettingsFromServer() { name = "";//name received from server } public void setMyActivity(SettingsSystem activity) { this.activity = activity; } public void applyChanges() { if (this.activity != null) { this.activity.ApplySettings(); } } } 

Then in your MyActivity class you can do this:

public void setConfigActivity() { this.config.setMyActivity(this); } 

Now your Config Instance has SettingsSystem.

NOTE

Methods shouldn't begin with Capital letter. This is against Java coding standard. Also you say its MyActivity.java but show it as SettingsSystem Would need consistency here as that will not compile.

Also it is bad practice to have circular dependencies. Might want to rethink how the two classes interact with each other.

Sign up to request clarification or add additional context in comments.

4 Comments

settings are updated in Config class after every 2 seconds. I want to update these settings as soon as they arrive. Also how can I check in which activity I am right now?
Does the Activities get updated via the Config class?
How Activities are updated via class? Can you please explain? Im new to android and java staff.
You said you want Config to update MyActivity when it itself gets updated. Is your MyActivity class getting values from this update?
1

You can use EventBus to achieve what you want.

1.Create a new class named SettingsChangedEvent

final public class SettingsChangedEvent { /* Additional fields if needed */ } 

2.Add below code to getSettingsFromServer() method inside Config class

public void getSettingsFromServer() { name = ""; //name received from server // [ADD THIS] Notify to activity that settings are changed, // it should apply new settings EventBus.getDefault().postSticky(new SettingsChangedEvent()); } 

3.Add these methods to SettingsSystem activity

@Override protected void onStart() { super.onStart(); // Allow this activity to listen settings changed event EventBus.getDefault().register(this); } @Override protected void onStop() { // Disallow this activity from listening settings changed event EventBus.getDefault().unregister(this); super.onStop(); } @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) public void onSettingsChangedEvent(SettingsChangedEvent ev) { // When settings changed event is notified/posted from Config class, // the code inside this method will be executed. SettingsChangedEvent event = EventBus.getDefault().removeStickyEvent(SettingsChangedEvent.class); if (event != null) { // Apply new settings here ApplySettings(); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.