I have an android main activity showing some data which are taken from the database. Now I have a PreferenceActivity
which is shown when the user wants to change some preference, or delete the content of the database. But if this is the case, how do I force a 'reload' or something of the main activity from a class outside the main activity?
Asked
Active
Viewed 389 times
1

Alex
- 41,580
- 88
- 260
- 469
-
Use EventBus Or BroadCast . – tiny sunlight Dec 06 '15 at 16:51
-
Another alternative to this is just to reload your UI in `onResume()` or `onStart()`, as those will be called as your activity is coming back to the foreground. – CommonsWare Dec 06 '15 at 17:01
-
I just tried to overwrite `onResume()` and got a runtime error without logcat output... – Alex Dec 06 '15 at 17:06
2 Answers
1
Set a OnSharedPreferenceChangeListener
listener in your MainActivity
to automatically detect any changes in the preferences.
MainActivity.java
public class MainActivity extends ... {
private SharedPreferences settings;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settings = PreferenceManager.getDefaultSharedPreferences(this);
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Refresh display
refreshDisplay();
}
};
// Register the listener on the SharedPreferences
settings.registerOnSharedPreferenceChangeListener(listener);
// Other code
}
public void refreshDisplay() {
// Retrieve entries from sharedPreferences & display them, e.g
String prefValue1 = settings.getString("key1", "default value 1");
String prefValue2 = settings.getString("key2", "default value 2");
// Update UI with these values
}
}
EDIT:
Here is how your PreferenceActivity
should be:
public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// R.xml.settings refers to the XML layout file named "settings"
// in your res/xml directory
addPreferencesFromResource(R.xml.settings);
}
}

Mohammed Aouf Zouag
- 17,042
- 4
- 41
- 67
-
But what is `key`? And how to 'reload' the main activity? Just put in `this.onCreate()`? – Alex Dec 06 '15 at 16:53
-
`key` refers to the preference item that changed. & yes, put this in **MainActivity**'s `onCreate` method. – Mohammed Aouf Zouag Dec 06 '15 at 16:56
-
Does not seem to work. I put a log output in that method, but I do not see the log output if I open my 'settings' dialog and do something in it. Do I need those methiods as well? Put in a caller or something? – Alex Dec 06 '15 at 16:59
-
Don't forget to call `setOnSharedPreferenceChangeListener()` on the `SharedPreferences`. Otherwise, the listener will not get used. – CommonsWare Dec 06 '15 at 17:02
-
On what `sharedPreference`? In the piece of code, where I want the main activity to 'reload' its content, is a class derived from `PreferenceActivity`. There is no `sharedPreference`! – Alex Dec 06 '15 at 17:06
-
It does not seem that there is a method named `refreshDisplay`. Or do I have to write this on my own? – Alex Dec 06 '15 at 17:10
-
@Alex it's defined below `onCreate`. Take a closer look: I added it to make the code more readable. You can discard it if you want. – Mohammed Aouf Zouag Dec 06 '15 at 17:11
-
But still, the method `onSharedPreferenceChanged` is never called!! I checked that the `SettingsActivity` is as you suggested, and it is! – Alex Dec 06 '15 at 17:13
-
Can you update your post with the full code that you have right now ? – Mohammed Aouf Zouag Dec 06 '15 at 17:14
-
No I cannot. Way too much code. I need to start another project and remove bit by bit to make it postable in a forum like this. This will take a long time. Why must this all be so extremely complicated? Maybe I will just making 'guesses' and hope to solve my problem alone... – Alex Dec 06 '15 at 17:17
-
-
Do you have a `settings.xml` file in the main/res/xml/ directory ? – Mohammed Aouf Zouag Dec 06 '15 at 17:29
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97129/discussion-between-alex-and-sparta). – Alex Dec 06 '15 at 17:35
-1
If you just want to recharge an activity, you can do something like this:
finish();
startActivity(getIntent());

Robert
- 1,192
- 1
- 10
- 20