From a9b9f8e2a846c5e7bfb5c615dc2a997111a711ad Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 12 Jul 2021 22:39:44 +0900 Subject: [PATCH] Wrap show keyboard logic in try catch. Fixes austinhuang0131/barinsta#1482 --- .../fragments/comments/RepliesFragment.java | 22 ++++++++++++++----- .../java/awais/instagrabber/utils/Utils.java | 20 ++++++++++------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/comments/RepliesFragment.java b/app/src/main/java/awais/instagrabber/fragments/comments/RepliesFragment.java index bbb12a7e..1122e749 100644 --- a/app/src/main/java/awais/instagrabber/fragments/comments/RepliesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/comments/RepliesFragment.java @@ -59,7 +59,9 @@ public class RepliesFragment extends Fragment { @Override public void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); - viewModel = new ViewModelProvider(getParentFragment()).get(CommentsViewerViewModel.class); + final Fragment parentFragment = getParentFragment(); + if (parentFragment == null) return; + viewModel = new ViewModelProvider(parentFragment).get(CommentsViewerViewModel.class); final Bundle bundle = getArguments(); if (bundle == null) return; final Serializable serializable = bundle.getSerializable(ARG_PARENT); @@ -117,10 +119,13 @@ public class RepliesFragment extends Fragment { @Override public void onDestroy() { super.onDestroy(); - viewModel.clearReplies(); + if (viewModel != null) { + viewModel.clearReplies(); + } } private void setupObservers() { + if (viewModel == null) return; viewModel.getCurrentUserId().observe(getViewLifecycleOwner(), currentUserId -> { long userId = 0; if (currentUserId != null) { @@ -151,7 +156,8 @@ public class RepliesFragment extends Fragment { final Bundle bundle = getArguments(); if (bundle == null) return; final boolean focusInput = bundle.getBoolean(ARG_FOCUS_INPUT); - if (focusInput && viewModel.getRepliesParent() != null && viewModel.getRepliesParent().getUser() != null) { + if (focusInput && viewModel.getRepliesParent() != null) { + viewModel.getRepliesParent().getUser(); binding.commentText.setText(String.format("@%s ", viewModel.getRepliesParent().getUser().getUsername())); Utils.showKeyboard(binding.commentText); } @@ -167,8 +173,9 @@ public class RepliesFragment extends Fragment { break; case ERROR: binding.swipeRefreshLayout.setRefreshing(false); - if (!TextUtils.isEmpty(listResource.message)) { - Snackbar.make(binding.getRoot(), listResource.message, Snackbar.LENGTH_LONG).show(); + final String message = listResource.message; + if (!TextUtils.isEmpty(message)) { + Snackbar.make(binding.getRoot(), message, Snackbar.LENGTH_LONG).show(); } break; case LOADING: @@ -188,6 +195,7 @@ public class RepliesFragment extends Fragment { } private void setupAdapter(final long currentUserId) { + if (viewModel == null) return; final Context context = getContext(); if (context == null) return; commentsAdapter = new CommentsAdapter( @@ -215,7 +223,9 @@ public class RepliesFragment extends Fragment { final Context context = getContext(); if (context == null) return; final LinearLayoutManager layoutManager = new LinearLayoutManager(context); - final RecyclerLazyLoader lazyLoader = new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> viewModel.fetchReplies()); + final RecyclerLazyLoader lazyLoader = new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> { + if (viewModel != null) viewModel.fetchReplies(); + }); Helper.setupList(context, binding.comments, layoutManager, lazyLoader); } diff --git a/app/src/main/java/awais/instagrabber/utils/Utils.java b/app/src/main/java/awais/instagrabber/utils/Utils.java index 0d5cda1d..78f30eef 100644 --- a/app/src/main/java/awais/instagrabber/utils/Utils.java +++ b/app/src/main/java/awais/instagrabber/utils/Utils.java @@ -355,14 +355,18 @@ public final class Utils { // } public static void showKeyboard(@NonNull final View view) { - final Context context = view.getContext(); - if (context == null) return; - final InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm == null) return; - view.requestFocus(); - final boolean shown = imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); - if (!shown) { - Log.e(TAG, "showKeyboard: System did not display the keyboard"); + try { + final Context context = view.getContext(); + if (context == null) return; + final InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); + if (imm == null) return; + view.requestFocus(); + final boolean shown = imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); + if (!shown) { + Log.e(TAG, "showKeyboard: System did not display the keyboard"); + } + } catch (Exception e) { + Log.e(TAG, "showKeyboard: ", e); } }