Creating Settings activity

We will start by creating an activity for Settings:

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

public class SettingsActivity extends RxAppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

Then, we will add it to AndroidManifest.xml:

<activity android:name=".SettingsActivity" />

Next, we will use PreferenceFragment by forming a SettingsFragment class inside the SettingsActivity to provide the preferences UI:

public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}

The R.xml.preferences resource doesn't exist yet, but we will create it promptly. Now we need to add a fragment to activity by calling the following:

getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();

In the end, the whole activity is very compact:

public class SettingsActivity extends RxAppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}

public static class
SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.198.94