如何从PreferenceActivity获取getFragmentManager

时间:2014-11-21 08:54:03

标签: android android-fragments android-preferences

我使用按钮PreferenceActivity创建了Preference out并指定了她的活动。 我需要清除用户名和密码(我只清除了Preference)并转到片段"授权"。我的片段来自

android.support.v4.app.Fragment;
android.support.v4.app.FragmentManager;

我无法从getFragmentManager();

获得android.support.v4.app.FragmentManagerPreferenceActivity
 public class PrefActivity extends PreferenceActivity {
        Preference out;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref);
            out = findPreference("logout");
            out.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    FragmentManager fragmentManager = getFragmentManager();
                    Fragment auth = new AuthDialog();
                    Bundle arg = new Bundle();
                    arg.putString("login", "");
                    arg.putString("password", "");
                    auth.setArguments(arg);
                    fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    fragmentManager.beginTransaction().replace(R.id.content_frame, auth).commit();
                    SharedPreferences sPref = PreferenceManager.getDefaultSharedPreferences(getApplication());
                    sPref.edit().clear().commit();
                    return false;
                }
            });
        }
    }

1 个答案:

答案 0 :(得分:0)

PreferenceActivityandroid.app.Activity延伸而不是android.support.v4.app.Activity。听起来很奇怪,没有办法使用带有PreferenceActivity的v4片段。您最好的选择是使用this library