Conflict between PreferenceFragment and onOptionsItemSelected

1

It seems that I have a conflict between PreferenceFragment and onOptionsItemSelected, I am trying to send several ListPreference variables from UsbSettingActivity to MainActivity using Intents, the problem is only the first data arrives, using the debug of the SDK in this activity observed that only the data is available:

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 );
   }
 }

Can someone tell me what my error is ?, the data in listPreference is loaded and displayed correctly, if I replace the other variables except for the first one by text string they all arrive correctly to me

    
asked by W1ll 27.11.2018 в 02:25
source

1 answer

1

The problem is that you are defining the same variable names within the onCreate() method, therefore they will only have value within this method.

Use the variables that you define in the class:

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 = "";
...
...

Make the change so that the variables can be sent with their respective value via intent :

 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();                      mconfigdatabit = SPconfigdatabit2.getValue();

        ListPreference SPconfigbitstop2 = (ListPreference) findPreference("config_bit_stop");
        //String mconfigbitstop = SPconfigbitstop2.getValue();
        mconfigbitstop = SPconfigbitstop2.getValue();

        ListPreference SPmconfigparity2 = (ListPreference) findPreference("config_parity");
        //String mconfigparity = SPmconfigparity2.getValue();                    
         mconfigparity = SPmconfigparity2.getValue();

        ListPreference SPconfigfontsize2 = (ListPreference) findPreference("config_font_size");
        //String mconfigfontsize = SPconfigfontsize2.getValue();            
         mconfigfontsize = SPconfigfontsize2.getValue();

        ListPreference SPconfigconsolamode2 = (ListPreference) findPreference("config_consola_mode");
        //String mconfigconsolamode = SPconfigconsolamode2.getValue();        
        mconfigconsolamode = SPconfigconsolamode2.getValue();

        ListPreference SPconfigfindelinea2 = (ListPreference) findPreference("config_fin_delinea");
        //String mconfigfindelinea = SPconfigfindelinea2.getValue();
        mconfigfindelinea = SPconfigfindelinea2.getValue();

        TwoStatePreference SPconfigenableeco2 = (TwoStatePreference) findPreference("config_enable_eco");
        Boolean mconfigenableecoX = SPconfigenableeco2.isChecked();
        //String mconfigenableeco = String.valueOf(mconfigenableecoX);
        mconfigenableeco = String.valueOf(mconfigenableecoX);

        ListPreference SPconfigfilesave2 = (ListPreference) findPreference("config_filesave");
        //String mconfigfilesave = SPconfigfilesave2.getValue();   
        mconfigfilesave = SPconfigfilesave2.getValue();
    }
    
answered by 27.11.2018 / 04:29
source