Note that there are some explanatory texts on larger screens.

plurals
  1. POOnPreferenceChangeListener
    primarykey
    data
    text
    <p>I have a <code>EditText</code> in a preference menu that allows me to edit a URL address. The problem is when I get the preference value in the mainActivity is not getting updated right away after I click OK in the Preference Menu. Not sure how to fix this problem. I tried a bunch of ideas and finally decided to ask. </p> <pre><code>public class PreferencesActivityTest extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.radio_preferences); PreferenceManager.setDefaultValues(PreferencesActivityTest.this, R.xml.radio_preferences, false); EditTextPreference editPref =(EditTextPreference)findPreference("MyText"); editPref.setOnPreferenceChangeListener( new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { if (newValue.toString().length() &gt; 0) { return true; } // If now create a message to the user Toast.makeText(PreferencesActivityTest.this, "Invalid Input", Toast.LENGTH_SHORT).show(); return false; } }); } } </code></pre> <p>PS: This code updates the <code>newValue</code> to what I enter in the <code>EditTextPreference</code>, doesn't carry the new value to the <code>MainActivity</code> until I modify it again...</p> <p>UPDATE: In <code>OnResume()</code> I can see that the value is updated with the one that I modified in the <code>PreferenceActivityTest</code> from <code>EditTextPreference</code>. What I'm trying to do is to pass this <code>newValue</code> into the <code>SetDataSource("")</code>.</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_radio); initializeMediaPlayer(); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_edit: editURL(); // make a Dialog or show an Activity return true; } } private void initializeMediaPlayer() { PreferenceManager.setDefaultValues(this, R.xml.radio_preferences, false); SharedPreferences pref =PreferenceManager.getDefaultSharedPreferences(this); String radioPath = pref.getString("MyText", "default value"); // Toast.makeText(this, radioPath, Toast.LENGTH_SHORT).show(); try { radioPlayer.reset(); // radioPlayer.setDataSource("http://31.xx.xxx"); // Toast.makeText(this, radioPath, Toast.LENGTH_SHORT).show(); radioPlayer.setDataSource(radioPath); } catch { } } public void editURL() { stopPlaying(); startActivity(new Intent(getBaseContext(), PreferencesActivityTest.class)); } </code></pre> <p>I am doing something fundamentally wrong but I need help. Thank you in advance !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload