From 6ab65a9b740fe119e47dee32d0c0a6bce582f4c2 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 23 Feb 2020 20:00:35 +0530 Subject: Ensure insets are dispatched to all fragments Signed-off-by: Harsh Shandilya --- .../com/wireguard/android/activity/MainActivity.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/src/main/java/com/wireguard/android/activity/MainActivity.java b/app/src/main/java/com/wireguard/android/activity/MainActivity.java index e70af6f1..5bc563e6 100644 --- a/app/src/main/java/com/wireguard/android/activity/MainActivity.java +++ b/app/src/main/java/com/wireguard/android/activity/MainActivity.java @@ -9,11 +9,17 @@ import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentManager.OnBackStackChangedListener; import androidx.fragment.app.FragmentTransaction; import androidx.appcompat.app.ActionBar; import android.view.Menu; import android.view.MenuItem; +import android.view.View; +import android.view.View.OnApplyWindowInsetsListener; +import android.view.ViewGroup; +import android.view.WindowInsets; import android.widget.LinearLayout; import com.wireguard.android.R; @@ -21,6 +27,9 @@ import com.wireguard.android.fragment.TunnelDetailFragment; import com.wireguard.android.fragment.TunnelEditorFragment; import com.wireguard.android.model.Tunnel; +import java.util.List; +import java.util.Objects; + /** * CRUD interface for WireGuard tunnels. This activity serves as the main entry point to the * WireGuard application, and contains several fragments for listing, viewing details of, and @@ -68,6 +77,16 @@ public class MainActivity extends BaseActivity isTwoPaneLayout = findViewById(R.id.master_detail_wrapper) instanceof LinearLayout; getSupportFragmentManager().addOnBackStackChangedListener(this); onBackStackChanged(); + // Dispatch insets on back stack change + // This is required to ensure replaced fragments are also able to consume insets + ((ViewGroup) findViewById(android.R.id.content)).setOnApplyWindowInsetsListener((OnApplyWindowInsetsListener) (v, insets) -> { + final FragmentManager fragmentManager = getSupportFragmentManager(); + fragmentManager.addOnBackStackChangedListener(() -> { + final List fragments = fragmentManager.getFragments(); + Objects.requireNonNull(fragments.get(fragments.size() - 1).getView()).dispatchApplyWindowInsets(insets); + }); + return insets; + }); } @Override -- cgit v1.2.3