title: How to keep scrolling position in ScrollView after rotating the screen in fragment with LiveDate object loaded
body: <p>I have a fragment which is to display a lyrics of a song which is loaded from room databse based on its given id. </p>
  
  <p>I'd like to preserve scrolling position after rotating the screen. Now after rotating the song is loaded again from db and the view is on the very top regardless of the scrolling position befor the rotating.</p>
  
  <p>I thought that I can save scrolling position in <code>onSaveInstanceState</code> some bundle in <code>onCreateView()</code> use command on <code>mSongDisplayScrollView.scrollTo(x, y)</code></p>
  
  <p>The fragment code:</p>
  
  <pre class="lang-java prettyprint-override"><code>
  public class SongDisplayFragment extends Fragment {
  
      private Song mSongToDisplay;
  
      private ScrollView mSongDisplayScrollView;
      private TextView mSongTitleTextView;
      private RecyclerView mSongLyricsRecyclerView;
  
      private GuitarSongbookViewModel mGuitarSongbookViewModel;
  
      public static final String SONG_ID_KEY = "SONG_ID_KEY";
  
  
      public SongDisplayFragment() {
  
      }
  
      public static SongDisplayFragment newInstance(Long songId) {
          SongDisplayFragment songDisplayFragment = new SongDisplayFragment();
          Bundle arguments = new Bundle();
          arguments.putLong(SONG_ID_KEY, 
          songDisplayFragment.setArguments(arguments);
          return songDisplayFragment;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setHasOptionsMenu(false);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View view = inflater.inflate(R.layout.fragment_song_display, container, false);
  
          mSongDisplayScrollView = view.findViewById(R.id.song_display_scroll_view);
          mSongLyricsRecyclerView = view.findViewById(R.id.lyrics_rv_);
          mSongTitleTextView = view.findViewById(R.id.
  
          mGuitarSongbookViewModel = ViewModelProviders.of(this).get(GuitarSongbookViewModel.class);
  
          final SongDisplayAdapter songDisplayAdapter = new SongDisplayAdapter(getContext());
  
          Long songId = null;
          if (getArguments().containsKey(SONG_ID_KEY)) {
              songId = getArguments().getLong(SONG_ID_KEY);
          }
  
          if (songId != null) {
              final Long finalSongId = songId;
  
  
              mGuitarSongbookViewModel.getSongById(songId).observe(this, new Observer&lt;Song&gt;() {
                  @Override
                  public void onChanged(@Nullable final Song song) {
                      mSongToDisplay = song;
                      mSongTitleTextView.setText(mSongToDisplay.getMTitle());
                      songDisplayAdapter.setSong(song);
  
                  }
              });
  
  
          }
  
  
          mSongLyricsRecyclerView.setAdapter(songDisplayAdapter);
          mSongLyricsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
  
          return view;
      }
  
  }
  
  </code></pre>
  
  <p>The fragment XML:</p>
  
  <pre class="lang-xml prettyprint-override"><code>
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".fragments.SongDisplayFragment"&gt;
  
      &lt;ScrollView
          android:id="@+id/song_display_scroll_view"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          app:layout_constraintBottom_toTopOf="@+id/autoscroll_bar"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="1.0"
          tools:layout_editor_absoluteX="138dp"&gt;
  
          &lt;LinearLayout
  
              android:id="@+id/son_display_linear_layout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"&gt;
  
              &lt;TextView
                  android:id="@+id/displayed_song_title_txt_"
                  style="@style/TitleOfDisplayedSong"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginStart="16dp"
                  android:layout_marginTop="8dp"
                  android:text="@string/title_placeholder" /&gt;
  
              &lt;androidx.recyclerview.widget.RecyclerView
                  android:id="@+id/lyrics_rv_"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_marginTop="8dp"
                  android:layout_marginBottom="12dp"
                  android:nestedScrollingEnabled="false" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/ScrollView&gt;
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p>The fragment displays a tiltle and lyrics of a song in a textView and RecyclerView which adapter class code is:</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.example.guitarsongbook.adapters;
  
  import android.content.Context;
  import android.graphics.Rect;
  import android.text.Html;
  import android.util.TypedValue;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  
  import androidx.annotation.NonNull;
  import androidx.recyclerview.widget.GridLayoutManager;
  import androidx.recyclerview.widget.RecyclerView;
  
  import com.example.guitarsongbook.R;
  import com.example.guitarsongbook.daos.SongChordJoinDao;
  import com.example.guitarsongbook.model.Chord;
  import com.example.guitarsongbook.model.Song;
  
  import java.util.ArrayList;
  import java.util.List;
  
  
  public class SongDisplayAdapter extends RecyclerView.Adapter&lt;SongDisplayAdapter.SongViewHolder&gt; {
  
      private Context context;
      private final LayoutInflater mInflater;
  
      private Song mSong;
      private ArrayList&lt;String&gt; mLyrics;
  
      public SongDisplayAdapter(Context context) {
          this.context = context;
          mInflater = LayoutInflater.from(context);
      }
  
      @NonNull
      @Override
      public SongViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View itemView = mInflater.inflate(R.layout.song_lyrics_rv_item, parent, false);
          return new SongDisplayAdapter.SongViewHolder(itemView);
      }
  
      public void setSong(Song song){
          mSong = song;
          mLyrics = mSong.getMLyrics();
          notifyDataSetChanged();
      }
  
  
  
      @Override
      public void onBindViewHolder(@NonNull SongViewHolder holder, int position) {
          holder.bindTo(position);
      }
  
      @Override
      public int getItemCount() {
          if (mLyrics != null)
              return mLyrics.size();
          else return 0;
      }
  
      public class SongViewHolder extends RecyclerView.ViewHolder {
  
          private final TextView mLyricsLineTextView;
  
  
          public SongViewHolder(@NonNull View itemView) {
              super(itemView);
              mLyricsLineTextView = itemView.findViewById(R.id.song_lyric_line_txt_);
          }
  
          public void bindTo(int position) {
              if (mSong != null) {
                  mLyricsLineTextView.setText(Html.fromHtml(mLyrics.get(position)));
  
  
  
              } else {
                  mLyricsLineTextView.setText(context.getString(R.string.no_song_label));
              }
          }
      }
  
  
  }
  
  </code></pre>
  
  <p>RecyclerView item XML:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
  
      &lt;TextView
          android:id="@+id/song_lyric_line_txt_"
          style="@style/LyricOfDisplayedSong"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginStart="16dp"
          android:layout_marginTop="4dp"
          android:layout_marginEnd="24dp"
          app:layout_constraintEnd_toStartOf="@+id/song_chord_line_txt_"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          tools:text="placeholder text" /&gt;
  
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p>I think that the problem is that after rotating the screen song has to be loaded again by observer's methon <code>onChange()</code> so there is a short moment that there is nothing to display for scrollView. I'd like to find some solution how to return to the old position after again loading the song.</p>