title: Scroll to top and then go back On Back Button Pressed in Recyclerview
body: <p>I have a <a href="/questions/tagged/recyclerveiw" class="post-tag" title="show questions tagged &#39;recyclerveiw&#39;" rel="tag">recyclerveiw</a>. When user press back i want recyclerview to scroll to top. And again if user click back button then go to previous activity.</p>
  
  <pre><code>  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
      if (Integer.parseInt(android.os.Build.VERSION.SDK) &gt; 5
              &amp;&amp; keyCode == KeyEvent.KEYCODE_BACK
              &amp;&amp; event.getRepeatCount() == 0) {
      //    if(recyclerview == onFirstPosition){
      //      goBack; }
          onBackPressed();
          return true;
      }
      return super.onKeyDown(keyCode, event);
  }
  
  
  @Override
  public void onBackPressed() {
      RecyclerView.SmoothScroller smoothScroller = new 
  LinearSmoothScroller(getApplicationContext()) {
          @Override protected int getVerticalSnapPreference() {
              return LinearSmoothScroller.SNAP_TO_START;
          }
      };
      smoothScroller.setTargetPosition(0);
      LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView
              .getLayoutManager();
      layoutManager.startSmoothScroll(smoothScroller);
  }
  </code></pre>
  
  <p>Now i can scroll to top with this code but don't know how to go back if user is on first item.</p>