Can a PreferenceFragment class be called before executing a method?

0

In my application before executing the Intent that is inside onOptionsItemSelected I need to read the ListPreference values, for that I am using MyPreferenceFragment, but when I try to execute this inside onOptionsItemSelected I get an error, someone could tell me how to solve this

Here is my code:

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

    //Recibiendo datos por Intent desde MainActivity
    Bundle bundle = getIntent().getExtras();
    bundle = getIntent().getExtras();

    mconfigbaudrate = bundle.getString("mconfigbaudratex");
    mconfigdatabit = bundle.getString("mconfigdatabitx");
    mconfigbitstop = bundle.getString("mconfigbitstopx");

}


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

       // Leendo parametros de ListPreference para enviarlos por Intent a MainActivity
        ListPreference SPconfigbaudrate2 = (ListPreference) findPreference("config_baud_rate");
        mconfigbaudrate = SPconfigbaudrate2.getValue();

        ListPreference SPconfigdatabit2 = (ListPreference) findPreference("config_data_bit");
        mconfigdatabit = SPconfigdatabit2.getValue();

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

    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == android.R.id.home) {

        //Enviando datos por Intent a MainActivity
        Intent intent2 = new Intent(this, MainActivity.class);

        intent2.putExtra("mconfigbaudratex",mconfigbaudrate);
        intent2.putExtra("mconfigdatabitx",mconfigdatabit);
        intent2.putExtra("mconfigbitstopx", mconfigbitstop);

        startActivityForResult(intent2, 0);
    }
    return super.onOptionsItemSelected( item );
}

}

    
asked by W1ll 28.11.2018 в 01:44
source

0 answers