title: How to store Pagination data in ViewModel Android?
body: <p>On rotating the device I am losing the previous page loaded page data. How can I resolve this?</p>
  
  <p>With below implementation, only the data of the current page is persisted in the ViewModel but all other pages are lost. Any solution for this?</p>
  
  <p>API response (Page 1)
  <a href="http://www.mocky.io/v2/5ed20f9732000052005ca0a6" rel="nofollow noreferrer">http://www.mocky.io/v2/5ed20f9732000052005ca0a6</a></p>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>class NewsApiViewModel(application: Application) : AndroidViewModel(application) {
  
      val topHeadlines = MutableLiveData&lt;TopHeadlines&gt;()
      var networkLiveData = MutableLiveData&lt;Boolean&gt;()
  
      fun fetchTopHeadlines(pageNumber: Int) {
          if (!getApplication&lt;Application&gt;().hasNetworkConnection()) {
              networkLiveData.value = false
          }
          val destinationService = ServiceBuilder.buildService(DestinationService::class.java)
          val requestCall = destinationService.getTopHeadlines(AppConstants.COUNTRY_INDIA, AppConstants.PAGE_SIZE, pageNumber, AppConstants.NEWS_API_KEY)
          requestCall.enqueue(object : Callback&lt;TopHeadlines&gt; {
              override fun onFailure(call: Call&lt;TopHeadlines&gt;, t: Throwable) {
                  Log.e("ANKUSH", "$t ")
              }
  
              override fun onResponse(call: Call&lt;TopHeadlines&gt;, response: Response&lt;TopHeadlines&gt;) {
                  if (response.isSuccessful) {
                      topHeadlines.postValue(response.body())
                  }
              }
          })
      }
  
  }
  </code></pre>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>fun observeAndUpdateData() {
          activity.viewModel.topHeadlines.observeForever {
              isDataLoading = false
              checkAndShowLoader(isDataLoading)
              if (AppConstants.STATUS_OK == it.status) {
                  it.articles?.let { articles -&gt;
                      if (articles.size &lt; AppConstants.PAGE_SIZE) {
                          activity.isAllDataLoaded = true
                      }
                      activity.adapter.updateData(articles)
                  }
              }
          }
      }
  
  fun getTopHeadlines(pageNumber: Int) {
          if (activity.isAllDataLoaded) return
          isDataLoading = true
          checkAndShowLoader(isDataLoading)
          activity.viewModel.fetchTopHeadlines(pageNumber)
      }
  </code></pre>