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