3232import androidx .core .app .NotificationCompat ;
3333import androidx .core .app .ServiceCompat ;
3434import androidx .core .math .MathUtils ;
35+ import androidx .fragment .app .DialogFragment ;
3536import androidx .fragment .app .Fragment ;
3637import androidx .fragment .app .FragmentManager ;
3738import androidx .preference .PreferenceManager ;
@@ -113,6 +114,7 @@ public class RouterActivity extends AppCompatActivity {
113114 private boolean selectionIsDownload = false ;
114115 private boolean selectionIsAddToPlaylist = false ;
115116 private AlertDialog alertDialogChoice = null ;
117+ private FragmentManager .FragmentLifecycleCallbacks dismissListener = null ;
116118
117119 @ Override
118120 protected void onCreate (final Bundle savedInstanceState ) {
@@ -142,6 +144,27 @@ protected void onCreate(final Bundle savedInstanceState) {
142144 super .onCreate (savedInstanceState );
143145 Icepick .restoreInstanceState (this , savedInstanceState );
144146
147+ // FragmentManager will take care to recreate (Playlist|Download)Dialog when screen rotates
148+ // We used to .setOnDismissListener(dialog -> finish()); when creating these DialogFragments
149+ // but those callbacks won't survive a config change
150+ // Try an alternate approach to hook into FragmentManager instead, to that effect
151+ // (ref: https://stackoverflow.com/a/44028453)
152+ final FragmentManager fm = getSupportFragmentManager ();
153+ if (dismissListener == null ) {
154+ dismissListener = new FragmentManager .FragmentLifecycleCallbacks () {
155+ @ Override
156+ public void onFragmentDestroyed (@ NonNull final FragmentManager fm ,
157+ @ NonNull final Fragment f ) {
158+ super .onFragmentDestroyed (fm , f );
159+ if (f instanceof DialogFragment && fm .getFragments ().isEmpty ()) {
160+ // No more DialogFragments, we're done
161+ finish ();
162+ }
163+ }
164+ };
165+ }
166+ fm .registerFragmentLifecycleCallbacks (dismissListener , false );
167+
145168 if (TextUtils .isEmpty (currentUrl )) {
146169 currentUrl = getUrl (getIntent ());
147170
@@ -171,29 +194,10 @@ protected void onSaveInstanceState(@NonNull final Bundle outState) {
171194 protected void onStart () {
172195 super .onStart ();
173196
174- // FragmentManager will take care to recreate DialogFragments when screen rotates
175- // currently that's namely PlaylistDialog or DownloadDialog
176- // We used to .setOnDismissListener(dialog ->finish()); when creating those Dialogs
177- // but those callbacks won't survive a config change
178- // Try an alternate approach to hook into FragmentManager instead, to that effect
179- // (courtesy of https://stackoverflow.com/a/44028453)
180- final FragmentManager fm = getSupportFragmentManager ();
181- fm .registerFragmentLifecycleCallbacks (new FragmentManager .FragmentLifecycleCallbacks () {
182- @ Override
183- public void onFragmentViewDestroyed (@ NonNull final FragmentManager fm ,
184- @ NonNull final Fragment f ) {
185- super .onFragmentViewDestroyed (fm , f );
186- if (fm .getFragments ().isEmpty ()) {
187- // No more Dialog, we're done
188- finish ();
189- }
190- }
191- }, false );
192-
193197 // Don't overlap the DialogFragment after rotating the screen
194198 // If there's no DialogFragment, we're either starting afresh
195199 // or we didn't make it to PlaylistDialog or DownloadDialog before the orientation change
196- if (fm .getFragments ().isEmpty ()) {
200+ if (getSupportFragmentManager () .getFragments ().isEmpty ()) {
197201 // Start over from scratch
198202 handleUrl (currentUrl );
199203 }
@@ -203,6 +207,9 @@ public void onFragmentViewDestroyed(@NonNull final FragmentManager fm,
203207 protected void onDestroy () {
204208 super .onDestroy ();
205209
210+ if (dismissListener != null ) {
211+ getSupportFragmentManager ().unregisterFragmentLifecycleCallbacks (dismissListener );
212+ }
206213 disposables .clear ();
207214 }
208215
0 commit comments