Return from the nested Android PreferenceScreen to the previous preferences screen

I programmatically create a nested set of PreferenceScreen s. When the user plunges into one of these screens and selects something there, I want to return to the previous PreferenceScreen (i.e., to the "parent").

I could not find any information about this, and the finish() call did not complete the task, but rather returned from ALL PreferenceScreen instead of one.

+2
android nested preferencescreen
Feb 11 2018-11-11T00:
source share
3 answers

I just encounter a problem and just found a solution. You can override the onPreferenceTreeClick method in PreferenceActivity. call the preferenceScreen getDialog method, then call the dialog reject method. just like that, joy!

+1
Aug 09 2018-11-11T00:
source share

You can either leave the first open but not calling finish () on the one that launched the second. If this is not the case, you can save the state of the first screen and instead of “returning” you call it with the intention and load its previous state.

0
Feb 11 '11 at 18:01
source share

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, for example:

 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; } } 
0
Mar 27 2018-12-12T00:
source share



All Articles