0

I have an app for writing notes. There is a problem between EditNote.java and ViewNote.java. If I want to update an existing note, when I click the FAB to save the updated note, the text that appears is the old one, then I must close the current activity ViewNote.java and return back to it to see the new text that I wrote it before.

* EditNote.java:

package com.twitter.i_droidi.notah;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import io.realm.Realm;
import io.realm.RealmResults;

public class EditNote extends AppCompatActivity {

    private int id;
    private EditText editTitle;
    private EditText editBody;
    private Realm realm;
    private Notes note;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_note);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);

        editTitle = (EditText) findViewById(R.id.edit_title);
        editBody = (EditText) findViewById(R.id.edit_body);

        // GET THE SPECIFIED NOTE FORM THE DATABASE.
        this.realm = RealmController.with(this).getRealm();
        RealmResults<Notes> theNote = realm.where(Notes.class).findAll();
        note = theNote.get(id);
        editTitle.setText(note.getTitle());
        editBody.setText(note.getBody());

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(editTitle.getText() == null || editTitle.getText().toString().equals("") || editTitle.getText().toString().equals(" "))
                    Snackbar.make(findViewById(R.id.coordinatorlayout_edit_note), R.string.write_a_valid_note, Snackbar.LENGTH_SHORT).show();
                else
                {
                    RealmResults<Notes> results = realm.where(Notes.class).findAll();

                    realm.beginTransaction();
                    results.get(id).setTitle(editTitle.getText().toString());
                    results.get(id).setBody(editBody.getText().toString());
                    realm.commitTransaction();

                    Toast.makeText(EditNote.this, R.string.changes_saved, Toast.LENGTH_SHORT).show();

                    finish();
                }

            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

* ViewNote.java:

package com.twitter.i_droidi.notah;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import io.realm.Realm;
import io.realm.RealmResults;

public class ViewNote extends AppCompatActivity {

    private int id;
    private TextView viewTitle;
    private TextView viewBody;
    private Notes note;
    private Realm realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_note);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);

        viewTitle = (TextView) findViewById(R.id.view_title);
        viewBody = (TextView) findViewById(R.id.view_body);

        // GET THE SPECIFIED NOTE FROM THE DATABASE.
        this.realm = RealmController.with(this).getRealm();
        RealmResults<Notes> theNote = realm.where(Notes.class).findAll();
        note = theNote.get(id);
        viewTitle.setText(note.getTitle());
        viewBody.setText(note.getBody());

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(ViewNote.this, EditNote.class);
                intent.putExtra("id", id);
                startActivity(intent);
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}
Androider
  • 159
  • 11
  • Either use [startActivityForResult()](http://stackoverflow.com/questions/18243515/android-going-back-to-previous-activity-with-different-intent-value/18243541#18243541) or do your query in `onResume()`. Right now, you aren't telling `ViewNote.java` in anyway that the note has changed so it is still going to display whatever it retrieved in `onCreate()`. `onResume()` will run when it is first created and when you go back to it. Or, the first way you can tell it that there is an update in `onActivityResult()` – codeMagic Jul 05 '16 at 18:26
  • @Androider yes i was wrtting same as codeMagic – Nisarg Jul 05 '16 at 18:27
  • @codeMagic I saw the link you posted and also this (http://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android). And I wrote some code regarding this problem, but I still have the same problem. Can you please write some code that help me in my situation?! Thanks. – Androider Jul 05 '16 at 19:53
  • It would be better if you posted what you attempted along those lines and what happened/didn't happen including errors so that we can show you where you went wrong. – codeMagic Jul 05 '16 at 22:30
  • @codeMagic See this video that I made it for the problem: https://youtu.be/G0f9cCA5tLU. I hope it will be clear for you. SMALL EXPLANATION: **1.** When I edit the note and click "Save" button, the updated text doesn't appear (Only the old one). Then, in this point, when I click "Back" button below, also, the updated text doesn't appear in the MainActivity. If I enter the same note, it will shows the updated text. **2.** _If I want to update the note again, the problem will be the same, but in this step, if I click "Back" arrow in the Toolbar, the updated text appears in the MainActivity._ – Androider Jul 06 '16 at 00:07
  • @codeMagic The code is posted above, in the main post. – Androider Jul 06 '16 at 00:10
  • You haven't shown any code where you tried to use `startActivityForResult()` – codeMagic Jul 06 '16 at 00:14
  • @codeMagic I tried it before, but I got another problems in updating the text. I don't know how to use it correctly. So, I asked you to write some and clear code that may help me. – Androider Jul 06 '16 at 04:42

1 Answers1

0

I think I finally got it!

Just change (some) code in EditNote.java and ViewNote.java to the following code:

* EditNote.java:

...
...
...

Intent intent1 = new Intent(EditNote.this, ViewNote.class);
                    intent1.putExtra("id", id);
                    intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent1);
                    //finish();
                }

...
...
...

* ViewNote.java:

...
...
...

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(ViewNote.this, EditNote.class);
                intent.putExtra("id", id);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                //startActivityForResult(intent, 0);
                startActivity(intent);
            }
        });

...
...
...

@Override
    public void onBackPressed() {

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

        super.onBackPressed();
    }
Androider
  • 159
  • 11