1

I created a custom dialog preference, that has two spinners in order to select a specific interval of time (e.g. 2 seconds or 2 minutes ou 3 hours etc.) So, when I click my custom preference in the preference screen the custom dialog preference pops up and it shows the two spinners. However when I select one of the spinners (another dialog pops up - see 2nd Image) and if I change the screen's orientation the application is forced closed. :S This happens in Android 2.3.6 Gingerbread. I've tested with android 4.1 and it works fine, but the spinner is different, it's not in a dialog, it's in a combobox style.

I've put the code of all my functions in a try catch but I'm not being able to catch the exception. Here are the errors:

01-17 15:14:43.929: E/WindowManager(5029): Activity com.myapp.appdemo.EditPreferences has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405c1b00 that was originally added here
01-17 15:14:43.929: E/WindowManager(5029): android.view.WindowLeaked: Activity com.myapp.appdemo.EditPreferences has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405c1b00 that was originally added here
01-17 15:14:43.929: E/WindowManager(5029):  at android.view.ViewRoot.<init>(ViewRoot.java:263)

There is a solution to change the configchanges to this: android:configChanges="orientation|keyboardHidden", but I would like to know if there is another way to solve this problem since I've read on a lot of questions that this workaround may have some side effects.

I also tried to remove the spinners from the layout on the onDismiss function but I doesn't solve the problem. :S So using the function or not using the function doesn't solve the problem.

Here is the custom DialogPreference class:

    public class PreferenceCustomTime extends DialogPreference {

    private Context context;
    private Spinner spinner1, spinner2;
    private ArrayAdapter<CharSequence> adapter, adapterValue;

    public PreferenceCustomTime(Context context, AttributeSet attrs) {              
        super(context, attrs);      
        this.context = context;
        setPersistent(false);
        setDialogLayoutResource(R.layout.spinner_list_callpoint);
    }

    @Override
    protected void onBindDialogView(View view) {        
        spinner1 = (Spinner) view.findViewById(R.id.spinner1);
        spinner2 = (Spinner) view.findViewById(R.id.spinner2);

        adapter = ArrayAdapter.createFromResource(context, R.array.arrayTypeTime, android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //simple_spinner_dropdown_item simple_spinner_item
        spinner1.setAdapter(adapter);
        spinner1.setSelection(0);     
        adapterValue = ArrayAdapter.createFromResource(context, R.array.arrayValueTime1, android.R.layout.simple_spinner_dropdown_item);
        adapterValue.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapterValue);          

        super.onBindDialogView(view);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        LayoutInflater inflater = LayoutInflater.from(context);
        LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.spinner_list_callpoint , null);
        linearLayout.removeView(spinner1);
        linearLayout.removeView(spinner2);
        super.onDismiss(dialog);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        if (positiveResult) {
            SharedPreferences.Editor editor = getEditor();
            editor.putString(getKey() + ".type", spinner1.getSelectedItem().toString());
            editor.putString(getKey() + ".value", spinner2.getSelectedItem().toString());
            editor.commit();       
        }
    } 
}
SnitramSD
  • 1,169
  • 3
  • 10
  • 18
  • in comment I'm not sure it will work for you, but try `linearLayout.removeAllViews()` instead `linearLayout.removeView(spinner).` – SteveR Mar 22 '13 at 03:26

1 Answers1

0

1st - it's not very nice to show a pop-up from a pop-up, if I were you I would try to make an expandable layout under the spinner or something.

2nd - you get window leaked error because the pop-up it's launched by the view underneath and when you change orientation, that connection is lost.

3rd - when you set android:configChanges="orientation|keyboardHidden in your XML, you tell Android you want to manage the orientation yourself so you might want to implement OnConfigurationChanged in your activity

MariusBudin
  • 1,277
  • 1
  • 10
  • 21
  • 1st - Do you know any example of that please? Im not understanding. 2nd - So there isn't any way to solve this problem with my custom DialogPreference interface? 3rd - A function like this: @Override public void onConfigurationChanged(Configuration newConfig){ super.onConfigurationChanged(newConfig); } – SnitramSD Jan 17 '13 at 17:16
  • The application is forced closed in android 2.3.6 Gingerbread. I've tested with android 4.1 and it works fine, but the spinner is different, it's not in a dialog, it's in a combobox style. How can I solve this? – SnitramSD Jan 17 '13 at 17:33
  • It's exactly what I was trying to tell you to do, since ICS spinners are displayed like that.It doesn't crash because the view it's not displayed in a pop-up on top of your dialog, therefore doesn't lose any connection. if I were you I would try to port that down to your min version.I'm sure you can google for a downporting library. But as I said before you can avoid that by overriding onConfigurationChanged, check de documentation – MariusBudin Jan 18 '13 at 09:58
  • If I use the onConfigurationChanged function among with the xml attribute of android:configChanges="orientation|keyboardHidden" is it safe to use? I read in post it had side-effects is this true? What kind of code should I use in the onConfigurationChanged function to make it safe? I only called this line and it worked: super.onConfigurationChanged(); but is it safe? My application won't crash? – SnitramSD Jan 18 '13 at 10:41