33import static org .schabi .newpipe .util .Localization .assureCorrectAppLanguage ;
44
55import android .os .Bundle ;
6+ import android .text .TextUtils ;
67import android .util .Log ;
78import android .view .Menu ;
89import android .view .MenuItem ;
4041
4142import java .util .concurrent .TimeUnit ;
4243
44+ import icepick .Icepick ;
45+ import icepick .State ;
46+
4347/*
4448 * Created by Christian Schabesberger on 31.08.15.
4549 *
@@ -77,20 +81,37 @@ public class SettingsActivity extends AppCompatActivity implements
7781 private View searchContainer ;
7882 private EditText searchEditText ;
7983
84+ // State
85+ @ State
86+ String searchText ;
87+ @ State
88+ boolean wasSearchActive ;
89+
8090 @ Override
8191 protected void onCreate (final Bundle savedInstanceBundle ) {
8292 setTheme (ThemeHelper .getSettingsThemeStyle (this ));
8393 assureCorrectAppLanguage (this );
94+
8495 super .onCreate (savedInstanceBundle );
96+ Icepick .restoreInstanceState (this , savedInstanceBundle );
97+ final boolean restored = savedInstanceBundle != null ;
8598
8699 final SettingsLayoutBinding settingsLayoutBinding =
87100 SettingsLayoutBinding .inflate (getLayoutInflater ());
88101 setContentView (settingsLayoutBinding .getRoot ());
89- initSearch (settingsLayoutBinding );
102+ initSearch (settingsLayoutBinding , restored );
90103
91104 setSupportActionBar (settingsLayoutBinding .settingsToolbarLayout .toolbar );
92105
93- if (savedInstanceBundle == null ) {
106+ if (restored ) {
107+ // Restore state
108+ if (this .wasSearchActive ) {
109+ setSearchActive (true );
110+ if (!TextUtils .isEmpty (this .searchText )) {
111+ this .searchEditText .setText (this .searchText );
112+ }
113+ }
114+ } else {
94115 getSupportFragmentManager ().beginTransaction ()
95116 .replace (R .id .settings_fragment_holder , new MainSettingsFragment ())
96117 .commit ();
@@ -101,6 +122,12 @@ protected void onCreate(final Bundle savedInstanceBundle) {
101122 }
102123 }
103124
125+ @ Override
126+ protected void onSaveInstanceState (@ NonNull final Bundle outState ) {
127+ super .onSaveInstanceState (outState );
128+ Icepick .saveInstanceState (this , outState );
129+ }
130+
104131 @ Override
105132 public boolean onCreateOptionsMenu (final Menu menu ) {
106133 final ActionBar actionBar = getSupportActionBar ();
@@ -175,7 +202,10 @@ protected void onDestroy() {
175202 //////////////////////////////////////////////////////////////////////////*/
176203 //region Search
177204
178- private void initSearch (final SettingsLayoutBinding settingsLayoutBinding ) {
205+ private void initSearch (
206+ final SettingsLayoutBinding settingsLayoutBinding ,
207+ final boolean restored
208+ ) {
179209 searchContainer =
180210 settingsLayoutBinding .settingsToolbarLayout .toolbar
181211 .findViewById (R .id .toolbar_search_container );
@@ -207,7 +237,19 @@ private void initSearch(final SettingsLayoutBinding settingsLayoutBinding) {
207237 .map (parser ::parse )
208238 .forEach (searcher ::add );
209239
210- searchFragment = new PreferenceSearchFragment (searcher );
240+ if (restored ) {
241+ searchFragment = (PreferenceSearchFragment ) getSupportFragmentManager ()
242+ .findFragmentByTag (PreferenceSearchFragment .NAME );
243+ if (searchFragment != null ) {
244+ // Hide/Remove the search fragment otherwise we get an exception
245+ // when adding it (because it's already present)
246+ hideSearchFragment ();
247+ }
248+ }
249+ if (searchFragment == null ) {
250+ searchFragment = new PreferenceSearchFragment ();
251+ }
252+ searchFragment .setSearcher (searcher );
211253 }
212254
213255 private void prepareSearchConfig () {
@@ -228,43 +270,56 @@ private void prepareSearchConfig() {
228270
229271 public void setMenuSearchItem (final MenuItem menuSearchItem ) {
230272 this .menuSearchItem = menuSearchItem ;
273+
274+ // Ensure that the item is in the correct state when adding it. This is due to
275+ // Android's lifecycle (the Activity is recreated before the Fragment that registers this)
276+ if (menuSearchItem != null ) {
277+ menuSearchItem .setVisible (!isSearchActive ());
278+ }
231279 }
232280
233281 public void setSearchActive (final boolean active ) {
282+ if (DEBUG ) {
283+ Log .d (TAG , "setSearchActive called active=" + active );
284+ }
285+
234286 // Ignore if search is already in correct state
235287 if (isSearchActive () == active ) {
236288 return ;
237289 }
238290
239- if (DEBUG ) {
240- Log .d (TAG , "setSearchActive called active=" + active );
241- }
291+ wasSearchActive = active ;
242292
243293 searchContainer .setVisibility (active ? View .VISIBLE : View .GONE );
244294 if (menuSearchItem != null ) {
245295 menuSearchItem .setVisible (!active );
246296 }
247297
248- final FragmentManager fm = getSupportFragmentManager ();
249298 if (active ) {
250- fm .beginTransaction ()
299+ getSupportFragmentManager ()
300+ .beginTransaction ()
251301 .add (FRAGMENT_HOLDER_ID , searchFragment , PreferenceSearchFragment .NAME )
252302 .addToBackStack (PreferenceSearchFragment .NAME )
253303 .commit ();
254304
255305 KeyboardUtil .showKeyboard (this , searchEditText );
256306 } else if (searchFragment != null ) {
257- fm .beginTransaction ().remove (searchFragment ).commit ();
258- fm .popBackStack (
259- PreferenceSearchFragment .NAME ,
260- FragmentManager .POP_BACK_STACK_INCLUSIVE );
307+ hideSearchFragment ();
308+ getSupportFragmentManager ()
309+ .popBackStack (
310+ PreferenceSearchFragment .NAME ,
311+ FragmentManager .POP_BACK_STACK_INCLUSIVE );
261312
262313 KeyboardUtil .hideKeyboard (this , searchEditText );
263314 }
264315
265316 resetSearchText ();
266317 }
267318
319+ private void hideSearchFragment () {
320+ getSupportFragmentManager ().beginTransaction ().remove (searchFragment ).commit ();
321+ }
322+
268323 private void resetSearchText () {
269324 searchEditText .setText ("" );
270325 }
@@ -279,7 +334,8 @@ private void onSearchChanged() {
279334 }
280335
281336 if (searchFragment != null ) {
282- searchFragment .updateSearchResults (this .searchEditText .getText ().toString ());
337+ searchText = this .searchEditText .getText ().toString ();
338+ searchFragment .updateSearchResults (searchText );
283339 }
284340 }
285341
0 commit comments