21
21
import android .content .Context ;
22
22
import android .content .DialogInterface ;
23
23
import android .os .AsyncTask ;
24
+ import android .os .Bundle ;
25
+ import android .os .Parcelable ;
24
26
import android .support .v7 .widget .AppCompatEditText ;
25
27
import android .text .TextUtils ;
26
28
import android .util .AttributeSet ;
37
39
38
40
public final class CountryListSpinner extends AppCompatEditText implements
39
41
View .OnClickListener , CountryListLoadTask .Listener {
42
+
43
+ private static final String KEY_SUPER_STATE = "KEY_SUPER_STATE" ;
44
+ private static final String KEY_COUNTRY_INFO = "KEY_COUNTRY_INFO" ;
45
+
40
46
private final String mTextFormat ;
41
47
private final DialogPopup mDialogPopup ;
42
48
private final CountryListAdapter mCountryListAdapter ;
43
49
private OnClickListener mListener ;
44
50
private String mSelectedCountryName ;
51
+ private CountryInfo mSelectedCountryInfo ;
45
52
46
53
public CountryListSpinner (Context context ) {
47
54
this (context , null , android .R .attr .spinnerStyle );
@@ -60,7 +67,32 @@ public CountryListSpinner(Context context, AttributeSet attrs, int defStyle) {
60
67
mTextFormat = "%1$s +%2$d" ;
61
68
mSelectedCountryName = "" ;
62
69
CountryInfo countryInfo = PhoneNumberUtils .getCurrentCountryInfo (getContext ());
63
- setSpinnerText (countryInfo .getCountryCode (), countryInfo .getLocale ());
70
+ setSelectedForCountry (countryInfo .getCountryCode (), countryInfo .getLocale ());
71
+ }
72
+
73
+ @ Override
74
+ public Parcelable onSaveInstanceState () {
75
+ Parcelable superState = super .onSaveInstanceState ();
76
+
77
+ Bundle bundle = new Bundle ();
78
+ bundle .putParcelable (KEY_SUPER_STATE , superState );
79
+ bundle .putParcelable (KEY_COUNTRY_INFO , mSelectedCountryInfo );
80
+
81
+ return bundle ;
82
+ }
83
+
84
+ @ Override
85
+ public void onRestoreInstanceState (Parcelable state ) {
86
+ if (!(state instanceof Bundle )) {
87
+ super .onRestoreInstanceState (state );
88
+ return ;
89
+ }
90
+
91
+ Bundle bundle = (Bundle ) state ;
92
+ Parcelable superState = bundle .getParcelable (KEY_SUPER_STATE );
93
+ mSelectedCountryInfo = bundle .getParcelable (KEY_COUNTRY_INFO );
94
+
95
+ super .onRestoreInstanceState (superState );
64
96
}
65
97
66
98
private static void hideKeyboard (Context context , View view ) {
@@ -70,19 +102,23 @@ private static void hideKeyboard(Context context, View view) {
70
102
}
71
103
}
72
104
73
- private void setSpinnerText (int countryCode , Locale locale ) {
105
+ private void setSelectedForCountry (int countryCode , Locale locale ) {
74
106
setText (String .format (mTextFormat , CountryInfo .localeToEmoji (locale ), countryCode ));
75
- setTag ( new CountryInfo (locale , countryCode ) );
107
+ mSelectedCountryInfo = new CountryInfo (locale , countryCode );
76
108
}
77
109
78
110
public void setSelectedForCountry (final Locale locale , String countryCode ) {
79
111
final String countryName = locale .getDisplayName ();
80
112
if (!TextUtils .isEmpty (countryName ) && !TextUtils .isEmpty (countryCode )) {
81
113
mSelectedCountryName = countryName ;
82
- setSpinnerText (Integer .parseInt (countryCode ), locale );
114
+ setSelectedForCountry (Integer .parseInt (countryCode ), locale );
83
115
}
84
116
}
85
117
118
+ public CountryInfo getSelectedCountryInfo () {
119
+ return mSelectedCountryInfo ;
120
+ }
121
+
86
122
@ Override
87
123
protected void onDetachedFromWindow () {
88
124
super .onDetachedFromWindow ();
@@ -169,7 +205,7 @@ public void run() {
169
205
public void onClick (DialogInterface dialog , int which ) {
170
206
final CountryInfo countryInfo = listAdapter .getItem (which );
171
207
mSelectedCountryName = countryInfo .getLocale ().getDisplayCountry ();
172
- setSpinnerText (countryInfo .getCountryCode (), countryInfo .getLocale ());
208
+ setSelectedForCountry (countryInfo .getCountryCode (), countryInfo .getLocale ());
173
209
dismiss ();
174
210
}
175
211
}
0 commit comments