Reputation: 326
I want to update main activity view when returning from the PreferenceActivity. I have read many threads about this and have implemented different approaches, but there is no way ...
Approach 1: Using startActivityForResult()
public boolean onOptionsItemSelected( MenuItem item ) {
switch( item.getItemId() ) {
case R.id.about:
AboutDialog about = new AboutDialog(this);
about.setTitle(getString(R.string.About));
about.show();
break;
case R.id.menu_settings:
startActivityForResult(new Intent(MainActivity.this, SettingsActivity.class),-1);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
this.charts.redraw();
super.onActivityResult(requestCode, resultCode, data);
}
onActivityResult() is never called. I have also tried using a valid requestCode but the result is the same.
Approach 2: Using OnSharedPreferenceChangeListener
public class MainActivity extends Activity implements OnItemSelectedListener, View.OnClickListener, OnCheckedChangeListener, OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
// ...
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
this.charts.redraw();
}
But again onSharedPreferenceChanged() is never called. I think it is not because of the well-known garbage collector issue since my listener is the MainActivity object and not a temporary object.
Approach 3: Using onResume()
public class MainActivity extends Activity implements OnItemSelectedListener, View.OnClickListener, OnCheckedChangeListener, OnSharedPreferenceChangeListener {
@Override
protected void onResume() {
this.charts.redraw();
super.onResume();
};
But again onResume() is not called as expected, indeed it is called when the PreferenceActivity is just started!!!??
Finally, the PreferenceActivity:
@SuppressWarnings("deprecation")
public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
initSummaries(this.getPreferenceScreen());
}
private void initSummaries( PreferenceGroup pg ) {
for( int i = 0; i < pg.getPreferenceCount(); i++ ) {
Preference p = pg.getPreference(i);
if( p instanceof PreferenceGroup )
initSummaries((PreferenceGroup)p);
else
setSummary(p);
}
}
private void setSummary(Preference pref) {
if( pref instanceof ListPreference ) {
ListPreference listPref = (ListPreference)pref;
pref.setSummary(listPref.getEntry());
}
}
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
Preference pref = findPreference(key);
setSummary(pref);
}
@Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
}
If I restart the application, the new values are displayed, so they are stored in the shared preferences correctly.
Am I missing something? This is quite frustrating ...
Upvotes: 2
Views: 822
Reputation: 4094
You don't seem to be calling setResult
on your PreferencesActivity
.
Upvotes: 1