2

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.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Lennert
  • 925
  • 1
  • 7
  • 21

3 Answers3

1

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!

Xuelong
  • 98
  • 1
  • 8
  • I am facing the same issues but can't understand how to do this with your indications. Could you add more infos or even a code if that's not too much to ask for? – gaborous Nov 04 '14 at 15:45
  • Found the full answer and I have put them in functions for easy usage: http://stackoverflow.com/a/26747403/1121352 – gaborous Nov 05 '14 at 00:03
0

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.

Unconn
  • 578
  • 4
  • 5
  • I have read about loading activities with an intent, but wasn't sure whether this is also possible for the PreferenceScreen as it is not derived from the Activity class. I am working on this right now, and will try it immediately though. – Lennert Feb 11 '11 at 18:52
  • It _IS_ possible but not how you should go about what you're trying to do. – Justin Buser Mar 26 '12 at 23:24
  • Yes this is possible but this will crash your app if you use too much of that nested menu because the nested menus will keep going deeper and deeper and the stack won't support it. – gaborous Nov 04 '14 at 15:31
0

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

}
Justin Buser
  • 2,813
  • 25
  • 32