To access this PreferenceScreen (Configuration)
First I have to access a list
and select the option and I just laza the options menu (Settings), I need that from the beginning the options menu is launched without having to enter the list, how can I do it?
This is the code:
public class SettingsActivity extends AppCompatPreferenceActivity {
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;
}
};
private static void bindPreferenceSummaryToValue(Preference preference) {
preference.setOnPreferenceChangeListener( sBindPreferenceSummaryToValueListener );
sBindPreferenceSummaryToValueListener.onPreferenceChange( preference,PreferenceManager
.getDefaultSharedPreferences( preference.getContext() )
.getString( preference.getKey(), "" ) );
}
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource( R.xml.pref_headers, target );
}
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals( fragmentName ) || ConfigCOM.class.getName().equals( fragmentName );
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class ConfigCOM extends PreferenceFragment {
@Override
public void onCreate(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" ) );
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity( new Intent( getActivity(), SettingsActivity.class ) );
return true;
}
return super.onOptionsItemSelected( item );
}
}
}