Return from nested Android PreferenceScreen to previous PreferenceScreen

ε祈祈猫儿з 提交于 2019-12-03 21:55:17

I just meet the problem and just found the solution. You can override the method onPreferenceTreeClick in PreferenceActivity. call the param preferenceScreen's getDialog method, then call the dialog's dismiss method. just like this, joy!

You can either leave the first one open buy not calling finish() on the one that launched the second one. If this does not do it, you could save the state of the first screen and instead of 'going back' you call it by an Intent and have it load its previous state.

Try something like:

package com.justinbuser.preferences;

import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.os.Bundle;

public class DynamicSettingsActivity extends PreferenceActivity{

    public DynamicSettingsActivity(){
        super();
    }

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        PreferenceScreen defaultRoot = getPreferenceManager().createPreferenceScreen(this);
        defaultRoot.setKey("root");

        PreferenceScreen videoScreen = getPreferenceManager().createPreferenceScreen(this);
        videoScreen.setTitle(R.string.video_chooser_title);
        videoScreen.setSummary(R.string.video_chooser_summary);
        videoScreen.setKey(getString(R.string.video));
        //The following MUST be called before creating videoPref
        defaultRoot.addPreference(videoScreen);

        videoPref = new VideoChooserPreferenceCategory(mContext, videoScreen);
        videoPref.setTitle(R.string.video_chooser_dialog_title);
        videoPref.setSummary(R.string.video_chooser_dialog_summary);
        videoScreen.addPreference(videoPref);

        setPreferenceScreen(defaultRoot);
    }


}

With a custom preference category like:

package com.justinbuser.preferences;

import android.content.Context;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceClickListener;

public class VideoChooserPreferenceCategory extends PreferenceCategory implements OnPreferenceClickListener {

    private PreferenceScreen parentScreen;
    private final Context mContext;

    public VideoChooserPreferenceCategory(Context context) {
        super(context);
        mContext = context;
    }

    public VideoChooserPreferenceCategory(Context context, PreferenceScreen preferenceScreen) {
        super(context);
        parentScreen = preferenceScreen;
        mContext = context;
    }

    @Override
    protected void onAttachedToActivity() {

        if(this.getPreferenceCount() > 0)
        {
            this.removeAll();
        }
        Preference videoPref = new Preference(mContext);
        videoPref.setKey(videoIds[videoId]);
        videoPref.setTitle("Video Title");
        videoPref.setSummary("Video Description");
        videoPref.setOnPreferenceClickListener(this);
        addPreference(videoPref);
        super.onAttachedToActivity();
    }

    public boolean onPreferenceClick(Preference preference) {
        parentScreen.getDialog().dismiss();
        this.callChangeListener(preference.getKey());
        return true;
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!