Nabeel Rajabali
Lurker
Hello. I'm currently following this tutorial for trying to create a settings page using the preference library that will lead to another preference screen when a preference is selected. Basically I want my settings page to lead to another advanced settings page when a preference is selected.
What I currently have done is my settings fragment, my settings activity, the layout for my preferences and the layout for my settings activity.
activity_settings.xml
SettingsActivity.java
preferences.xml
SettingsFragment.java
feedback.xml
FeedbackFragment.java
I'm trying to use the onPreferenceStartFragment method to replace the layout with my new fragment. However, I'm currently struggling with figuring out how to attach a preference to this listener and how this code works.
This is my first post in this forum so any feedback on how I can improve my posts would also be great.
What I currently have done is my settings fragment, my settings activity, the layout for my preferences and the layout for my settings activity.
activity_settings.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SettingsActivity">
<!--frame layout for displaying
our preference fragment-->
<FrameLayout
android:id="@+id/idFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
SettingsActivity.java
Java:
package com.example.navapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceManager;
import android.os.Bundle;
import android.os.Bundle;
import androidx.preference.Preference;
import android.preference.PreferenceFragment;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
public class SettingsActivity extends AppCompatActivity implements
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// below line is to change
// the title of our action bar.
getSupportActionBar().setTitle("Settings");
// below line is used to check if
// frame layout is empty or not.
if (findViewById(R.id.idFrameLayout) != null) {
if (savedInstanceState != null) {
return;
}
// below line is to inflate our fragment.
getFragmentManager().beginTransaction().add(R.id.idFrameLayout, new SettingsFragment()).commit();
PreferenceManager.
}
}
@Override
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
final Bundle args = pref.getExtras();
final Fragment fragment = this.getSupportFragmentManager().getFragmentFactory().instantiate(
this.getClassLoader(),
fragment.setArguments(args);
fragment.setTargetFragment(caller, 0);
int setupSettingsFragment = getFragmentManager().findFragmentByTag("FeedbackFragment").getId();
this.getSupportFragmentManager().beginTransaction()
.replace(setupSettingsFragment, fragment)
.addToBackStack(null)
.commit();
return true;
}
}
preferences.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:key="switch"
android:summaryOff="Switch off"
android:summaryOn="Switch on"
android:title="Switch Preference"
android:fragment="com.example.navapp.FeedbackFragment"/>
<Preference
android:key="feedback"
android:title="Send feedback"
android:summary="Report technical issues or suggest new features"/>
android:fragment="com.example.navapp.FeedbackFragment"/>
</PreferenceScreen>
SettingsFragment.java
Java:
package com.example.navapp;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import androidx.annotation.Nullable;
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// below line is used to add preference
// fragment from our xml folder.
addPreferencesFromResource(R.xml.preferences);
}
}
feedback.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="feedback"
android:title="Send feedback to devs"
android:summary="Report technical issues or suggest new features"/>
</PreferenceScreen>
FeedbackFragment.java
Java:
package com.example.navapp;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import androidx.annotation.Nullable;
public class FeedbackFragment extends PreferenceFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// below line is used to add preference
// fragment from our xml folder.
addPreferencesFromResource(R.xml.feedback);
}
}
I'm trying to use the onPreferenceStartFragment method to replace the layout with my new fragment. However, I'm currently struggling with figuring out how to attach a preference to this listener and how this code works.
This is my first post in this forum so any feedback on how I can improve my posts would also be great.