0

I am creating a notes app, and I've all but finished it. My app starts on the main activity, which shows a recylcerView displaying all of the saved notes. To create a new note, you press a button, which sends you to another activity where you write your note. You then press a button that saves the note as an instance of the Note class and sends that object instance back to the main activity where it updates the recyclerView.

My problem is, every time I press the save button for my note, it just updates the Note instance instead of creating an entirely new one. How do I get it to create a new instance of the Note class so that I can have more than one saved note?

Here is my code for the save button:

Intent intent = new Intent(AddNoteActivity.this, MainActivity.class);

String mTitle = title.getText().toString();
String mContent = content.getText().toString();

intent.putExtra("notePar", new Note(mTitle, mContent));

startActivity(intent);

Here is my code for the mainactivity:

Intent intent = getIntent();

Note sentParcNote = intent.getParcelableExtra("notePar");

if(sentParcNote != null) {
   notes.add(sentParcNote);
}
  • 1
    Your current code isn't sufficient for me to determine where the problem might be. Please provide a [mcve]. – Code-Apprentice Jan 28 '22 at 19:45
  • Also, how are you storing the notes to display in the recycler view? Do you have a list of `Note` objects or a database that stores them or a file or what? – Code-Apprentice Jan 28 '22 at 19:46

1 Answers1

0

You are using startActivity(intent) to navigate from AddNoteActivity to MainActivity, this method is used to start a new activity, which means, the system will create a new instance of MainActivity class and put it at the top of the activity stack. This way you will always have 0 or 1 note (when sentParcNote != null)

I would suggest to use startActivityForResult when you navigate from MainActivity to AddNoteActivity and call setResult in your AddNoteActivity

Example:

MainActivity:

Declare this static int at the top of your class (e.g: just before onCreate method)

private static final int ADD_NOTE_ACTIVITY_REQUEST_CODE = 2;

Then add this piece of code on your button action to start AddNoteActivity:

Intent addNoteIntent = new Intent(this, AddNoteActivity.class);
startActivityForResult(addNoteIntent, ADD_NOTE_ACTIVITY_REQUEST_CODE);

Then to catch the new note from AddNoteActivity

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == ADD_NOTE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {

                Note sentParcNote = data.getParcelableExtra("notePar");

                if(sentParcNote != null) {
                      notes.add(sentParcNote);
                }
            }
        }
    }

AddNoteActivity:

add this piece of code to your save button action

String mTitle = title.getText().toString();
String mContent = content.getText().toString();

Intent intent = new Intent();
intent.putExtra("notePar", new Note(mTitle, mContent));
setResult(RESULT_OK, intent);
// finish closes the current activity which means in this case it goes back to the MainActivity
finish();

I would suggest using local storage to save your notes otherwise if you restart the app you will always have 0 notes.