Here I have an example of the code for showing an app preference. The first code is a class which extends PreferenceFragment and the second is a class which extends PreferenceActivity.
PreferenceScreen xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="my_nickname"
android:title="Enter your nickname"
android:summary="Here you need to enter your nickname if you want to change it">
</EditTextPreference>
<ListPreference
android:key="color_key"
android:title="Favorite color"
android:summary="What is your favorite color to change your color preference"
android:entries="@array/favorite_colors"
android:entryValues="@array/colors_numbers"
android:defaultValue="1"/>
<CheckBoxPreference
android:key="notification_key"
android:title="I want to receive a notification"
android:summary="If you check this you will receive a notification"
android:defaultValue="false"/>
</PreferenceScreen>
Extend PreferenceFragment class:
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class CustomPreferenceWithFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Extend PreferenceActivity class:
import android.preference.PreferenceActivity;
import android.os.Bundle;
public class CustomActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new CustomPreferenceWithFragment())
.commit();
}
}
QUESTIONS:
- What is exactly the job of the class which extends PreferencedFragment and what is the job of the class which extends PreferenceActivity?
- What is a meaning of android.R.id.content?
- I know that fragment has to be connected with an activity but why here fragment is not connected with Activity class(extends Activity or AppCompatActivity) instead of PreferenceActivity which is placed here?