Search code examples
androidpreferenceactivity

Reach activity from DialogPreference


I have a DialogPreference that I open from a PreferenceActivity. I would like to close the PreferenceActivity when the DialogPreference is closed. In the class SeekBarPreference extends DialogPreference, I have

@Override
protected void onDialogClosed(boolean positiveResult) {


}

Here I need a reference to the SettingsActivity so I can finish() it.

In comparaison, when I manage a DialogFragment, I can use the Callback Method :

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    parentActivity = ((MapActivity) activity);
}

How can I do to close my settingsActivity from DialogPreference???

Thanks in advance!


Solution

  • While creating the dialog preference, you are taking in the context right?

    Then cast the context to Activity and call finish.

    //Field Variable
    private Context mContext;
    
    //Your Constructor
    public  DialogPreference(Context context){
        mContext=context
    }
    
    @Override
    protected void onDialogClosed(boolean positiveResult) {
        ((Activity)mContext).finish();
    }