udacity/Sunshine-Version-2

Use of getSupportFragmentManager() instead of getFragmentManager()

Opened this issue · 4 comments

To call the android.app.FragmentManager, shouldn't we be using,
getFragmentManager().beginTransaction().add(R.id.forecastListViewFragment,new
ForecastFragment()).commit();
Instead of,
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
Since we are using Fragment and not FragmentActivity?

getSupportFragmentManager() is relevant if you are supporting device below API 14 and not because we are using FragmentActivity. Anyway I don't see any usefulness of FragmentActivity at all.

@elmargomez thank you for the clarification.

@imageica you're very welcome.

Hoyyyyaaaaaaaaaaaaa Finaly I Fixed that

PreferenceActivity 

public class SettingsActivity extends PreferenceActivity
        implements Preference.OnPreferenceChangeListener {

// make a class to use PreferenceFragment because addPreferencesFromResource and findPreferencedeprecated in PreferenceActivity 
public static class Presf extends PreferenceFragment{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.pref_general);

        // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
        // updated when the preference changes.

      //make new class from SettingsActivity because bindPreferenceSummaryToValue is nonstatic class
//and now I can use that by new class
        SettingsActivity m = new SettingsActivity();
        m.bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));

    }

}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Add 'general' preferences, defined in the XML file
       
// finaly for setup Prefrence I used this and replace that by new Prefs Class, Hoya its Worked and data input via user is change and weather is update :D
        getFragmentManager().beginTransaction().replace(android.R.id.content, new Presf()).commit();


    }

    /**
     * Attaches a listener so the summary is always updated with the preference value.
     * Also fires the listener once, to initialize the summary (so it shows up before the value
     * is changed.)
     */
    private void bindPreferenceSummaryToValue(Preference preference) {
        // Set the listener to watch for value changes.
        preference.setOnPreferenceChangeListener(this);

        // Trigger the listener immediately with the preference's
        // current value.
        onPreferenceChange(preference,
                PreferenceManager
                        .getDefaultSharedPreferences(preference.getContext())
                        .getString(preference.getKey(), ""));
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();

        if (preference instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list (since they have separate labels/values).
            ListPreference listPreference = (ListPreference) preference;
            int prefIndex = listPreference.findIndexOfValue(stringValue);
            if (prefIndex >= 0) {
                preference.setSummary(listPreference.getEntries()[prefIndex]);
            }
        } else {
            // For other preferences, set the summary to the value's simple string representation.
            preference.setSummary(stringValue);
        }
        return true;
    }

}