0

The android official documentation recommends saving data in onStop(). but the problem is when I save the data in onStop the data gets saved multiple times on certain scenarios. For instance: "when I minimize the activity, return to the activity, and then minimize the activity again", this makes the data save multiple times as onStop is invoked any time the activity is minimized. i want the data to save only once because saving the data multiple times only spams the database.

This is the onStop() method

@Override
protected void onStop() {
    // call the superclass method first
    super.onStop();

    // save the note's current draft, because the activity is stopping
    // and we want to be sure the current note progress isn't lost.
    saveNote();
    MainActivity.showNoteOnRecyclerView(AddNoteActivity.this);
}

saveNote method:

public void saveNote() {
    try {
        //puts notes,title data from the UI elements to the model class
        if (title.getText().length() > 0 || note.getText().length() > 0) {
            NoteModel noteModel = new NoteModel(title.getText().toString(), note.getText().toString());

            //saves the noteModel data to the database
            DatabaseHandler databaseHandler = new DatabaseHandler(AddNoteActivity.this);
            databaseHandler.insertData(noteModel);
            Toast.makeText(this, "Note Saved", Toast.LENGTH_SHORT).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

updateNote Method:

public void updateNote() {
        try {
            //puts notes,title data from the UI elements to the model class and updates it in the database
            if (title.getText().length() > 0 || note.getText().length() > 0) {
                NoteModel noteModel = new NoteModel(title.getText().toString(), note.getText().toString());
                noteModel.setId(selectedId);
//               puts the updated data to the database
                DatabaseHandler databaseHandler = new DatabaseHandler(EditNoteActivity.this);
                databaseHandler.updateData(noteModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
Sky
  • 127
  • 9
  • 1
    Change the code of `saveNote()` so that it inserts the row to the table the 1st time it is invoked and it updates the row for any subsequent calls. – forpas Jul 23 '21 at 07:36
  • How does your saveNote() method work? When saving data, you want to consider new items vs items to be updated. – Eenvincible Jul 23 '21 at 07:36
  • @forpas pls can u elaborate on this – Sky Jul 23 '21 at 07:49
  • @Eenvincible I have added the saveNote() code to the question – Sky Jul 23 '21 at 07:49
  • Solid code design would lead you to create a single function like `insertOrUpdate()` then in it, do the mechanism that helps you tell whether an item already exists in the database or not; that will make it easier, than having two methods that do almost the exact same thing. – Eenvincible Jul 23 '21 at 08:47

2 Answers2

1

A simple solution.

Just add a member variable in your AddNoteActivity:

private var _added = false

then in onStop(), check _added:

if (!_added) {
   _added = true
   saveNote()
}

The _added flag is only created when activity is created, so it should persist even after onPause(), onStop() and onStart() onResume().

AIMIN PAN
  • 1,563
  • 1
  • 9
  • 13
0

The onStop() is called when ever that activity is no longer visible to the user. (Ex:- Minimizing the app as you have rightfully mentioned ). I've had a similar problem like this myself.

What I did to fix this was a very simple logical way. Before inserting the data I checked whether it was a previous entry with the same values. (Using a method I created inside the DatabaseHandler Class).

  • If there is a previous entry and the values are different Update it.
  • else If the entry is not there Insert it.
  • else do nothing.

But this might be a bit of a hassle if you are saving a lot of data. (In my case i was only saving a few entries). In that case try onUserLeaveHint()

Read More on onUserleaveHint()

A Similar thread you might be interested in

iWiiCK
  • 361
  • 5
  • 13