0

My app contains a series of long texts, and I'd like the user to be able to return to them at the point where they left off if they leave that View.

I've adapted the code from this answer for a ListView. The first Log is fine, but the second does not, which suggests to me that state is null.

Here's my StoryBodyActivity:

public class StoryBodyActivity extends AppCompatActivity {

    private TextView storyBodyTextView;
    private ScrollView storyBodyScrollView;
    Parcelable state;

    @Override
    public void onPause() {
        Log.d("pause", "saving listview state @ onPause");
        state = storyBodyTextView.onSaveInstanceState();
        super.onPause();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_story_body);

        Bundle extras = getIntent().getExtras();

        String story = extras.getString("story");

        setTitle(story);

        storyBodyTextView = (TextView) findViewById(R.id.story_body_text_view);
        storyBodyScrollView = (ScrollView) findViewById(R.id.story_body_scroll_view);

        DatabaseHelper db = new DatabaseHelper(this);

        String storyBody = db.getStoryBody(story);

        storyBodyTextView.setText(Html.fromHtml(storyBody));

        if(state != null) {
            Log.d("pause", "trying to restore textview state..");
            storyBodyTextView.onRestoreInstanceState(state);
        }
    }
}

I also need to ensure that the position of each individual long text is saved.

Here's the DatabaseHelper to show where the data is coming from:

public String getStoryBody(String story) {

        Log.i("test", "hello");

        String storyBody = "";

        // Select all query
        String selectQuery = "SELECT * FROM " + BOOKS + " WHERE title =  '" + story + "'";

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                storyBody = cursor.getString(4);
            } while (cursor.moveToNext());
        }
        return storyBody;
    }
Sebastian
  • 3,548
  • 18
  • 60
  • 95
  • I hope this help https://stackoverflow.com/questions/3014089/maintain-save-restore-scroll-position-when-returning-to-a-listview – David Nov 02 '17 at 17:15
  • Hi @David, thanks for your reply. That's for a ListView - would it work for a TextView as well? – Sebastian Nov 02 '17 at 17:32

0 Answers0