title: RecyclerView within viewpager lag when calling onResume
body: <p>I have an activity which contains a fragment that contains a tab layout alongside a viewpager. 
  Each viewpager has a fragment and each fragment has a recyclerview. </p>
  
  <p>To summarise: mainactivity (contains) -> mainFragment (contains) -> viewpager (contains) -> fragments (contains) -> recyclerview. </p>
  
  <p>When the app launch, the recyclerview scrolls smoothly, no lags or whatsoever. And even when I do a GPU profiling, the GPU bars generated on app launch are acceptable. </p>
  
  <p>The issue only happens when I go to another activity and go back to the viewpager (onResume) that the recyclerview becomes lag.</p>
  
  <p>To show you the issue at hand, here are some pictures on the GPU profiling: </p>
  
  <p><img src="https://i.stack.imgur.com/QJyNZ.png" width="250" /> 
  <img src="https://i.stack.imgur.com/HUwjs.png" width="250" /></p>
  
  <p>The image on the left is the initial app launch GPU profile when scrolled.</p>
  
  <p>The image in the right is the app GPU profile after invoking onResume 10 times and scrolled.</p>
  
  <p>As you can see, the adapter is not loading any images or doing any CPU/GPU consuming task in the UI thread - the adapter only loads an empty list hence not doing any expensive tasks. </p>
  
  <p>the onResume code for all the fragments is this: </p>
  
  <pre><code>@Override
  public void onResume() {
      super.onResume();
  }
  </code></pre>
  
  <p>Not even the recycler view is redrawn during onResume, the adapter is only "resumed" as as its last state by Android.</p>
  
  <p>So I'm at a lost. I was thinking if this has anything to do with memory leak, but after looking for days, I could not pinpoint the actual issue. </p>
  
  <p>Do advice. 
  Thanks in advance!</p>
  
  <p>Edit 1: 
  - Tried removing the viewpager and only loading the fragment containing the recyclerview. After doing the same thing by triggering OnResume 10 times, the app still has the overdraw problem, i.e: performance lag. </p>
  
  <p>Edit 2: 
  To clarify things futher, this are my implementations: </p>
  
  <p>The main fragment is inflated alongside the MainActivity via a method in MainActivity:</p>
  
  <pre><code>private void swapFragment(Class fragmentClass, String fragmentTag) {
      FragmentManager fm = getSupportFragmentManager();
      FragmentTransaction ft = fm.beginTransaction();
      Fragment fragment = fm.findFragmentByTag(fragmentTag);
  
      Fragment currentFragment = fm.getPrimaryNavigationFragment();
      if (currentFragment != null) {
          ft.detach(currentFragment);
      }
  
      try {
          if (fragment == null) {
              fragment = (Fragment) fragmentClass.newInstance();
              ft.add(R.id.flContent, fragment, fragmentTag);
          } else {
              ft.attach(fragment);
          }
  
      } catch (Exception e) {
          /*not in used
          empty*/
      }
  
      ft.setPrimaryNavigationFragment(fragment);
      ft.setReorderingAllowed(true);
      ft.commitNowAllowingStateLoss();
  }
  </code></pre>
  
  <p>After inflation, the MainFragment will inflate a Viewpager with a tab layout. The Viewpager uses a custom adapter: </p>
  
  <pre><code>public class NewsPagerAdapter extends FragmentStatePagerAdapter {
  
  private RealmResults&lt;Section&gt; catIds;
  
  public NewsPagerAdapter(FragmentManager fm, RealmResults&lt;Section&gt; catIds) {
      super(fm, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
      this.catIds = catIds;
  }
  
  @Override
  public Fragment getItem(int position) {
          return ArticleListFragment.newInstance(catIds.get(position).getSectionName(), true);
  }
  
  @Override
  public int getCount() {
      return catIds.size();
  }
  }
  </code></pre>
  
  <p>As you can see, the db that I am using is Realm, and during viewpager initiation, we query the db for "Section" results and pass the section name into an "ArticleListFragment" for inflation.</p>
  
  <p>Within ArticleListFragment is the recycler view. Inflation of the recycler view is such: </p>
  
  <pre><code>ArticleListAdapter articleAdapter = new ArticleListAdapter(articles, getContext(), this, realm);
  rvArticle.setLayoutManager(new LinearLayoutManager(getContext()));
  rvArticle.setAdapter(articleAdapter);
  </code></pre>
  
  <p>The recycler view adapter is such: </p>
  
  <pre><code>public class ArticleListAdapter extends RealmRecyclerViewAdapter&lt;ArticleNewCMS, ArticleListAdapter.ArticleHolder&gt; {
  private Context context;
  private Realm realm;
  private Fragment fragment;
  private RealmResults&lt;ArticleNewCMS&gt; articles;
  
  public ArticleListAdapter(@Nullable RealmResults&lt;ArticleNewCMS&gt; articles, Context context, @Nullable Fragment fragment, Realm realm) {
      super(articles, true);
      this.context = context;
      this.fragment = fragment;
      this.realm = realm;
      this.articles = articles;
  }
  
  @NonNull
  @Override
  public ArticleHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
      View view;
      switch (viewType) {
          case 0:
              view = LayoutInflater.from(context).inflate(!RealmHelper.NightmodeHelper.isNightMode(context) ? R.layout.item_rv_article_headline : R.layout.item_rv_article_headline_nightmode, viewGroup, false);
              break;
          case 7:
              view = LayoutInflater.from(context).inflate(R.layout.item_rv_article_lifestyle_headline, viewGroup, false);
              break;
          default:
              view = LayoutInflater.from(context).inflate(!RealmHelper.NightmodeHelper.isNightMode(context) ? R.layout.item_rv_article_compact : R.layout.item_rv_article_compact_nightmode, viewGroup, false);
      }
      return new ArticleHolder(view);
  }
  
  @Override
  public void onBindViewHolder(@NonNull final ArticleHolder holder, int position) {
  
  }
  
  public class ArticleHolder extends RecyclerView.ViewHolder {
      ArticleHolder(View itemView) {
          super(itemView);
          ButterKnife.bind(this, itemView);
      }
  }
  }
  </code></pre>
  
  <p>Of course, the adapter implementation was stripped down for simplicity. But it still poses the same issue: the scrolling lag still appears even when I remove all the views and bindings. </p>