Return from nested Android PreferenceScreen to previous PreferenceScreen

3k views Asked by At

I am programmatically creating a nested set of PreferenceScreens. When the user dives into one of these screens, and taps something in there, I want to go back to the previous PreferenceScreen, (i.e. the "parent").

I couldn't find any information on this, and calling finish() doesn't do the job but rather returns from ALL the PreferenceScreens instead of just one.

3

There are 3 answers

3
Unconn On

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.

0
Justin Buser On

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;
    }

}
2
Xuelong On

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!