Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Preference Fragment with a Navigation Drawer's Fragment

Hi I have an Android app that already uses a Navigation Drawer. My MainActivity extends Fragment Activity and my SettingFragment extends PreferenceFragment

Settings Fragment:

public class SettingsFragment extends PreferenceFragment {
    public SettingsFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.prefs);
    }
}

and my MainActivity:

PreferenceFragment preferenceFragment = new SettingsFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(android.R.id.content, preferenceFragment); // I'm getting an error here should be Fragment not PreferenceFragment
ft.commit();

How can I commit or go to the SettingsFragment()?

like image 695
AllenC Avatar asked Apr 13 '14 16:04

AllenC


People also ask

How to add DrawerLayout in android?

The drawer icon is displayed on all top-level destinations that use a DrawerLayout . To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.

What is PreferenceFragmentCompat?

A PreferenceFragmentCompat is the entry point to using the Preference library. This Fragment displays a hierarchy of Preference objects to the user. It also handles persisting values to the device. To retrieve an instance of android.


2 Answers

Wrap your current PreferenceFragment with a simple Fragment which does noting but opens prefenceFragment as follows

public class SettingsActivity extends Fragment {

   @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getActivity().getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new MyPreferenceFragment())
            .commit();
    }
    private class SettingsFragment extends PreferenceFragment {
      public SettingsFragment() {}

      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

       // Load the preferences from an XML resource
       addPreferencesFromResource(R.xml.prefs);
       }     
     }
}
like image 72
Muhammad Riyaz Avatar answered Oct 24 '22 11:10

Muhammad Riyaz


This Worked for me. Just keep in mind that this code will work with api leval 11 and higher.

In Activity use this code to add Fragment.

android.app.Fragment infoFragment = new InfoFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(android.R.id.content, infoFragment);
ft.commit();

And Your PreferenceFragment class will be look like this.

public class InfoFragment extends PreferenceFragment 
{
 /**
  * The fragment argument representing the section number for this
  * fragment.
 */
 private static final String ARG_SECTION_NUMBER = "section_number";

 /**
  * Returns a new instance of this fragment for the given section
  * number.
  */

  public static android.app.Fragment newInstance(int sectionNumber) 
  {
    InfoFragment fragment = new InfoFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
  }

  public InfoFragment() 
  {

  }
}
like image 2
hims_3009 Avatar answered Oct 24 '22 10:10

hims_3009