1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 00:20:32 +00:00

Try to amicably handle DialogFragment in FragmentManager when recreated from orientation change

- Handle finish() call instead of passing around callbacks to setOnDismissListener()
- Don't start over again if returning to DialogFragment before orientation change
This commit is contained in:
devlearner 2022-10-16 02:32:41 +08:00 committed by Stypox
parent 73e32889b6
commit b175774ad8
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23

View File

@ -31,6 +31,7 @@ import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.app.NotificationCompat;
import androidx.core.app.ServiceCompat;
import androidx.core.math.MathUtils;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.PreferenceManager;
@ -151,8 +152,33 @@ public class RouterActivity extends AppCompatActivity {
protected void onStart() {
super.onStart();
// FragmentManager will take care to recreate DialogFragments when screen rotates
// currently that's namely PlaylistDialog or DownloadDialog
// We used to .setOnDismissListener(dialog ->finish()); when creating those Dialogs
// but those callbacks won't survive a config change
// Try an alternate approach to hook into FragmentManager instead, to that effect
// (courtesy of https://stackoverflow.com/a/44028453)
final FragmentManager fm = getSupportFragmentManager();
fm.registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentViewDestroyed(@NonNull final FragmentManager fm,
@NonNull final Fragment f) {
super.onFragmentViewDestroyed(fm, f);
if (fm.getFragments().isEmpty()) {
// No more Dialog, we're done
finish();
}
}
}, false);
// Don't overlap the DialogFragment after rotating the screen
// If there's no DialogFragment, we're either starting afresh
// or we didn't make it to PlaylistDialog or DownloadDialog before the orientation change
if (fm.getFragments().isEmpty()) {
// Start over from scratch
handleUrl(currentUrl);
}
}
@Override
protected void onDestroy() {
@ -659,12 +685,12 @@ public class RouterActivity extends AppCompatActivity {
getThemeWrapperContext(),
List.of(new StreamEntity(info)),
playlistDialog -> {
playlistDialog.setOnDismissListener(dialog -> finish());
// to be handled by FragmentManager
// playlistDialog.setOnDismissListener(dialog ->finish());
playlistDialog.show(
this.getSupportFragmentManager(),
"addToPlaylistDialog"
);
final FragmentManager fm = getSupportFragmentManager();
playlistDialog.show(fm, "addToPlaylistDialog");
fm.executePendingTransactions();
}
),
throwable -> handleError(this, new ErrorInfo(
@ -684,7 +710,8 @@ public class RouterActivity extends AppCompatActivity {
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
final DownloadDialog downloadDialog = new DownloadDialog(this, result);
downloadDialog.setOnDismissListener(dialog -> finish());
// to be handled by FragmentManager since listener would be gone when recreated
// playlistDialog.setOnDismissListener(dialog ->finish());
final FragmentManager fm = getSupportFragmentManager();
downloadDialog.show(fm, "downloadDialog");