diff --git a/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageLayout.java b/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageLayout.java index ca0fe5763b61..cd51d7859eef 100644 --- a/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageLayout.java +++ b/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageLayout.java @@ -737,7 +737,7 @@ private void refreshFeed() { private void keepPosition(int prevScrollPosition, int prevRecyclerViewPosition, int prevRecyclerViewItemPosition) { - if ((mIsNewsOn != mIsShowNewsOn) || (mIsNewsOn && mIsShowOptin)) { + if (!mIsNewsOn || !mIsShowNewsOn || (mIsNewsOn && mIsShowOptin)) { return; } processFeed(); @@ -1186,25 +1186,28 @@ public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newStat int scrollY = mParentScrollView.getScrollY(); if (BraveActivity.getBraveActivity() != null - && BraveActivity.getBraveActivity().getActivityTab() != null) { + && BraveActivity.getBraveActivity().getActivityTab() != null + && mRecyclerView.getChildCount() > 0) { View firstChild = mRecyclerView.getChildAt(0); - int firstVisiblePosition = - mRecyclerView.getChildAdapterPosition(firstChild); - int verticalOffset = firstChild.getTop(); - - SharedPreferencesManager.getInstance().writeInt( - BRAVE_RECYCLERVIEW_OFFSET_POSITION - + BraveActivity.getBraveActivity() - .getActivityTab() - .getId(), - verticalOffset); - - SharedPreferencesManager.getInstance().writeInt( - BRAVE_RECYCLERVIEW_POSITION - + BraveActivity.getBraveActivity() - .getActivityTab() - .getId(), - firstVisiblePosition); + if (firstChild != null) { + int firstVisiblePosition = + mRecyclerView.getChildAdapterPosition(firstChild); + int verticalOffset = firstChild.getTop(); + + SharedPreferencesManager.getInstance().writeInt( + BRAVE_RECYCLERVIEW_OFFSET_POSITION + + BraveActivity.getBraveActivity() + .getActivityTab() + .getId(), + verticalOffset); + + SharedPreferencesManager.getInstance().writeInt( + BRAVE_RECYCLERVIEW_POSITION + + BraveActivity.getBraveActivity() + .getActivityTab() + .getId(), + firstVisiblePosition); + } } mFeedHash = SharedPreferencesManager.getInstance().readString( BravePreferenceKeys.BRAVE_NEWS_FEED_HASH, "");