1
0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2024-11-22 11:02:35 +01: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.NotificationCompat;
import androidx.core.app.ServiceCompat; import androidx.core.app.ServiceCompat;
import androidx.core.math.MathUtils; import androidx.core.math.MathUtils;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -151,7 +152,32 @@ public class RouterActivity extends AppCompatActivity {
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
handleUrl(currentUrl); // 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 @Override
@ -659,12 +685,12 @@ public class RouterActivity extends AppCompatActivity {
getThemeWrapperContext(), getThemeWrapperContext(),
List.of(new StreamEntity(info)), List.of(new StreamEntity(info)),
playlistDialog -> { playlistDialog -> {
playlistDialog.setOnDismissListener(dialog -> finish()); // to be handled by FragmentManager
// playlistDialog.setOnDismissListener(dialog ->finish());
playlistDialog.show( final FragmentManager fm = getSupportFragmentManager();
this.getSupportFragmentManager(), playlistDialog.show(fm, "addToPlaylistDialog");
"addToPlaylistDialog" fm.executePendingTransactions();
);
} }
), ),
throwable -> handleError(this, new ErrorInfo( throwable -> handleError(this, new ErrorInfo(
@ -684,7 +710,8 @@ public class RouterActivity extends AppCompatActivity {
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> { .subscribe(result -> {
final DownloadDialog downloadDialog = new DownloadDialog(this, 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(); final FragmentManager fm = getSupportFragmentManager();
downloadDialog.show(fm, "downloadDialog"); downloadDialog.show(fm, "downloadDialog");