Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

fix: Discovery WebViews Issues #1828

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.webkit.URLUtil;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand Down Expand Up @@ -39,6 +39,20 @@ public class WebViewDiscoverFragment extends BaseWebViewFragment {
protected FragmentWebviewDiscoveryBinding binding;
private ViewTreeObserver.OnScrollChangedListener onScrollChangedListener;

private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) {
@Override
public void handleOnBackPressed() {
if (binding.webview.canGoBack()) {
binding.webview.goBack();
} else {
// Disable the current callback to enable triggering the callback on the
// MainTabsDashboardFragment
onBackPressedCallback.setEnabled(false);
requireActivity().onBackPressed();
}
}
};

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand All @@ -51,7 +65,9 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initTitle();
setWebViewActionListener();
setWebViewBackPressListener();
requireActivity().getOnBackPressedDispatcher().addCallback(
getViewLifecycleOwner(), onBackPressedCallback
);

// Check for search query in extras
String searchQueryExtra = null;
Expand Down Expand Up @@ -118,18 +134,6 @@ public void onUserNotLoggedIn(@NonNull String courseId, boolean emailOptIn) {
}));
}

private void setWebViewBackPressListener() {
binding.webview.setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK && binding.webview.canGoBack()) {
binding.webview.goBack();
return true;
}
}
return false;
});
}

@Override
public void onSaveInstanceState(Bundle outState) {
if (URLUtil.isValidUrl(binding.webview.getUrl())) {
Expand Down Expand Up @@ -191,6 +195,18 @@ public void onStart() {
});
}

@Override
public void onResume() {
super.onResume();
onBackPressedCallback.setEnabled(true);
}

@Override
public void onPause() {
super.onPause();
onBackPressedCallback.setEnabled(false);
}

@Override
public void onStop() {
super.onStop();
Expand Down
Loading