@Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      final Activity a = getActivity();
      PackageManager pm = a.getPackageManager();
      setHasOptionsMenu(true);

      Log.v(TAG, "Prepping preferences: " + Preferences.getModeId());

      if (Preferences.isModePrivate()) {
        this.getPreferenceManager().setSharedPreferencesName(Preferences.FILENAME_PRIVATE);
        addPreferencesFromResource(R.xml.preferences_private_connection);

        loadHostPreferences(a);
        loadSecurityPreferences(a);
        loadOptionsPreferences(a);
        loadIdentificationPreferences(a);

      } else if (Preferences.isModeHosted()) {
        Log.v(TAG, "Prepping hosted preferences");
        this.getPreferenceManager().setSharedPreferencesName(Preferences.FILENAME_HOSTED);
        addPreferencesFromResource(R.xml.preferences_hosted_connection);

        loadIdentificationPreferencesHosted(a);

      } else if (Preferences.isModePublic()) {
        this.getPreferenceManager().setSharedPreferencesName(Preferences.FILENAME_PUBLIC);
        addPreferencesFromResource(R.xml.preferences_public_connection);
      } else {
        throw new RuntimeException("Unknown application mode");
      }

      mode = findPreference(Preferences.getKey(R.string.keyModeId));
      String[] modesReadable = getResources().getStringArray(R.array.profileIds_readable);
      mode.setSummary(modesReadable[Preferences.getModeId()]);
      mode.setOnPreferenceChangeListener(
          new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
              Preferences.setMode(Integer.parseInt((String) newValue));
              ActivityPreferencesConnection.modeSwitch =
                  true; // signal that ConnectionPreferences should be shown again after fragment is
                        // restored
              getFragmentManager()
                  .beginTransaction()
                  .remove(getFragmentManager().findFragmentByTag("preferences"))
                  .add(R.id.content_frame, new FragmentPreferences(), "preferences")
                  .commit();
              return false; // Don't save, setMode already did
            }
          });
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      final Activity a = getActivity();
      PackageManager pm = a.getPackageManager();
      modesReadable = getResources().getStringArray(R.array.profileIds_readable);

      Log.v(TAG, "Prepping preferences: " + Preferences.getModeId());

      if (Preferences.isModeFreeform()) {
        this.getPreferenceManager().setSharedPreferencesName(Preferences.FILENAME_FREEFORM);
        addPreferencesFromResource(R.xml.preferences_private);
      } else if (Preferences.isModePrivate()) {
        this.getPreferenceManager().setSharedPreferencesName(Preferences.FILENAME_PRIVATE);
        addPreferencesFromResource(R.xml.preferences_private);
      } else if (Preferences.isModeHosted()) {
        this.getPreferenceManager().setSharedPreferencesName(Preferences.FILENAME_HOSTED);
        addPreferencesFromResource(R.xml.preferences_hosted);
      } else if (Preferences.isModePublic()) {
        this.getPreferenceManager().setSharedPreferencesName(Preferences.FILENAME_PUBLIC);
        addPreferencesFromResource(R.xml.preferences_public);
      } else {
        throw new RuntimeException("Unknown application mode");
      }

      repo = findPreference("repo");
      mail = findPreference("mail");
      twitter = findPreference("twitter");
      version = findPreference("versionReadOnly");

      locatorDisplacement =
          (EditIntegerPreference)
              findPreference(Preferences.getKey(R.string.keyLocatorDisplacement));
      locatorDisplacement.setHint(
          Integer.toString(Preferences.getIntResource(R.integer.valLocatorDisplacement)));

      locatorInterval =
          (EditIntegerPreference) findPreference(Preferences.getKey(R.string.keyLocatorInterval));
      locatorInterval.setHint(
          Integer.toString(Preferences.getIntResource(R.integer.valLocatorInterval)));

      try {
        ver = pm.getPackageInfo(a.getPackageName(), 0).versionName;
      } catch (PackageManager.NameNotFoundException e) {
        ver = a.getString(R.string.na);
      }
      version.setSummary(ver);

      repo.setOnPreferenceClickListener(
          new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
              Intent intent = new Intent(Intent.ACTION_VIEW);
              intent.setData(Uri.parse(Preferences.getRepoUrl()));
              a.startActivity(intent);
              return false;
            }
          });

      mail.setOnPreferenceClickListener(
          new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
              Intent intent = new Intent(Intent.ACTION_SEND);
              intent.setType("message/rfc822");
              intent.putExtra(Intent.EXTRA_EMAIL, new String[] {Preferences.getIssuesMail()});
              intent.putExtra(Intent.EXTRA_SUBJECT, "OwnTracks (Version: " + ver + ")");
              a.startActivity(Intent.createChooser(intent, "Send Email"));
              return false;
            }
          });

      twitter.setOnPreferenceClickListener(
          new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
              Intent intent = new Intent(Intent.ACTION_VIEW);
              intent.setData(Uri.parse(Preferences.getTwitterUrl()));
              a.startActivity(intent);
              return false;
            }
          });

      setServerPreferenceSummary(this);

      Preference.OnPreferenceClickListener connectionListener =
          new Preference.OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
              Log.v(TAG, "startActivityForResult ActivityPreferencesConnection");
              Intent intent = new Intent(getActivity(), ActivityPreferencesConnection.class);
              intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
              getActivity().startActivityForResult(intent, REQUEST_CODE_CONNECTION);
              return true;
            }
          };

      // Fix toolbars for PreferenceScreens on demand
      Preference.OnPreferenceClickListener genericListener =
          new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(final android.preference.Preference preference) {
              if (!(preference instanceof PreferenceScreen)) return false;

              final Dialog dialog = ((PreferenceScreen) preference).getDialog();

              LinearLayout root = (LinearLayout) dialog.findViewById(android.R.id.list).getParent();
              final Toolbar bar =
                  (Toolbar)
                      LayoutInflater.from(preference.getContext())
                          .inflate(R.layout.toolbar, root, false);
              root.addView(bar, 0); // insert at top

              bar.setTitle(preference.getTitle());
              bar.setNavigationOnClickListener(
                  new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      dialog.dismiss();
                    }
                  });

              return false;
            }
          };
      findPreference("connectionScreen").setOnPreferenceClickListener(connectionListener);
      findPreference("reportingScreen").setOnPreferenceClickListener(genericListener);
      findPreference("notificationScreen").setOnPreferenceClickListener(genericListener);
      findPreference("advancedScreen").setOnPreferenceClickListener(genericListener);
      findPreference("informationScreen").setOnPreferenceClickListener(genericListener);
    }