Return from nested Android PreferenceScreen to previous PreferenceScreen
Asked Answered
M

3

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.

Mariel answered 11/2, 2011 at 15:10 Comment(0)
T
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!

Tory answered 9/8, 2011 at 9:13 Comment(2)
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?Telly
Found the full answer and I have put them in functions for easy usage: https://mcmap.net/q/358488/-how-to-return-to-the-previous-prefenencescreen-from-nested-preferencescreen-without-press-the-back-buttonTelly
P
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;
    }

}
Pipage answered 11/2, 2011 at 15:10 Comment(0)
K
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.

Kidding answered 11/2, 2011 at 18:1 Comment(3)
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.Mariel
It IS possible but not how you should go about what you're trying to do.Pipage
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.Telly

© 2022 - 2024 — McMap. All rights reserved.