It seems that I have a conflict between PreferenceFragment and onOptionsItemSelected , I am trying to send several ListPreference variables from UsbSettingActivity to the MainActivity using Intents, the problem is that I only get the first data, using the SDK debug in this activity, it observed that only the first data is available. fact:
mconfigbaudrate = 9600
Here is my code
public class UsbSettingActivity extends AppCompatPreferenceActivity {
final String setBaudRate = "" ;
public static String mconfigbaudrate = "";
public static String mconfigdatabit = "";
public static String mconfigbitstop = "";
public static String mconfigparity = "";
public static String mconfigfontsize = "";
public static String mconfigconsolamode = "";
public static String mconfigfindelinea = "";
public static String mconfigenableeco = "";
public static String mconfigfilesave = "";
private static Preference.OnPreferenceChangeListener
sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue( stringValue );
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null );
}else {
preference.setSummary( stringValue );
}
return true;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
setupActionBar();
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled( true );
}
}
private static void bindPreferenceSummaryToValue(Preference preference) {
preference.setOnPreferenceChangeListener( sBindPreferenceSummaryToValueListener );
sBindPreferenceSummaryToValueListener.onPreferenceChange( preference,PreferenceManager
.getDefaultSharedPreferences( preference.getContext() )
.getString( preference.getKey(), "" ) );
}
public static class MyPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_setup_com);
setHasOptionsMenu( true );
bindPreferenceSummaryToValue( findPreference( "config_baud_rate" ) );
bindPreferenceSummaryToValue( findPreference( "config_data_bit" ) );
bindPreferenceSummaryToValue( findPreference( "config_bit_stop" ) );
bindPreferenceSummaryToValue( findPreference( "config_parity" ) );
bindPreferenceSummaryToValue( findPreference( "config_flow_control" ) );
bindPreferenceSummaryToValue( findPreference( "config_font_size" ) );
bindPreferenceSummaryToValue( findPreference( "config_consola_mode" ) );
bindPreferenceSummaryToValue( findPreference( "config_fin_delinea" ) );
bindPreferenceSummaryToValue( findPreference( "config_filesave" ) );
ListPreference SPconfigbaudrate2 = (ListPreference) findPreference("config_baud_rate");
mconfigbaudrate = SPconfigbaudrate2.getValue();
ListPreference SPconfigdatabit2 = (ListPreference) findPreference("config_data_bit");
String mconfigdatabit = SPconfigdatabit2.getValue();
ListPreference SPconfigbitstop2 = (ListPreference) findPreference("config_bit_stop");
String mconfigbitstop = SPconfigbitstop2.getValue();
ListPreference SPmconfigparity2 = (ListPreference) findPreference("config_parity");
String mconfigparity = SPmconfigparity2.getValue();
ListPreference SPconfigfontsize2 = (ListPreference) findPreference("config_font_size");
String mconfigfontsize = SPconfigfontsize2.getValue();
ListPreference SPconfigconsolamode2 = (ListPreference) findPreference("config_consola_mode");
String mconfigconsolamode = SPconfigconsolamode2.getValue();
ListPreference SPconfigfindelinea2 = (ListPreference) findPreference("config_fin_delinea");
String mconfigfindelinea = SPconfigfindelinea2.getValue();
TwoStatePreference SPconfigenableeco2 = (TwoStatePreference) findPreference("config_enable_eco");
Boolean mconfigenableecoX = SPconfigenableeco2.isChecked();
String mconfigenableeco = String.valueOf(mconfigenableecoX);
ListPreference SPconfigfilesave2 = (ListPreference) findPreference("config_filesave");
String mconfigfilesave = SPconfigfilesave2.getValue();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
Intent intent2 = new Intent(this, MainActivity.class);
intent2.putExtra("mconfigbaudratex",mconfigbaudrate);
intent2.putExtra("mconfigdatabitx",mconfigdatabit);
intent2.putExtra("mconfigbitstopx", mconfigbitstop);
intent2.putExtra("mconfigparityx", mconfigparity);
intent2.putExtra("mconfigfontsizex", mconfigfontsize);
intent2.putExtra("mconfigconsolamodex", mconfigconsolamode);
intent2.putExtra("mconfigfindelineax", mconfigfindelinea);
intent2.putExtra("mconfigenableecox", mconfigenableeco);
intent2.putExtra("mconfigfilesavex", mconfigfilesave);
startActivityForResult(intent2, 0);
}
return super.onOptionsItemSelected( item );
}
}
Could someone tell me what my error is? The data in listPreference is loaded and displayed correctly, if I replace the other variables except the first one with a text string, they all arrive correctly
The problem is that you are defining the same variable names inside the method
onCreate()
, therefore they will only have a value inside this method.Use the variables you define in the class:
Make the change so that the variables can be sent with their respective value via
intent
: