Issue
Problem:
When I try to "reuse" so-to-speak a getIntent()
with extra values by passing data to it from a second activity, the extra values return null. So I want to pass NoteAdapter Intent Extras to the ViewNote class and I also want to pass EditNote Intent Extras to the ViewNote class using the same string keys.
Something like this: User chooses note > User Views Note > User Edits Note > User Views Note corrected.
What works:
Based upon a user selection of a RecyclerView
list of notes, the chosen note through a NoteAdapter
has extra details passed to an Intent
to a ViewNote
class which in turn has a Intent n = getIntent()
which these extras are received. Note details are loaded into the ViewNote
activity. If the user decides this note needs corrected or updated, then the ViewNote
class has an Intent
that passes extras to the EditNote
class. This all works great!
What doesn't work and What I'm trying to do:
I'm not sure whether this is allowed or can be done but I'm trying to pass extras back to the ViewNote
class from the EditNote
class using the same Intent n = getIntent()
in the ViewNote
class used earlier when a note was passed to it.
The ViewNote class getIntent()
:
Intent n = getIntent();
nNoteID = n.getIntExtra("ID", 0);
String nType = n.getStringExtra("Type");
String nSummary = n.getStringExtra("Summary");
String nSource = n.getStringExtra("Source");
String nAuthors = n.getStringExtra("Authors");
The EditNote Intent
to pass extras back to the ViewNote
Intent u = new Intent(EditNote.this, ViewNote.class);
u.putExtra("ID", vNoteID);
u.putExtra("Type", ty);
u.putExtra("Summary", su);
u.putExtra("Source", so);
u.putExtra("Authors",au);
EditNote.this.startActivity(u);
What I have tried:
I've read a few posts here on stackoverflow that appeared to be similar and tried changing the context (thinking maybe that was wrong), and tried using Bundle
but neither of those made any difference. I thought maybe the String
array values weren't passing so I just passed String
variables because the "ID" number as a String
seems to pass fine but the other values still return null
. I've read some of the Android Developer explanations about Intents and maybe I just don't understand Intents fully or maybe this just can't be done or is not how it should be done.
I'm working in the following:
- Intellij Idea 2021.1.1
- Java Android v.28 Min v.29 Max
- Gradle 4.1.2
Solution
The solution was exactly what David Wasser posted in the initial comments above. I needed to use the startActivityForResult
and follow the guidelines on how to do so. I did review the Android Development links and other examples online, but this video helped:
Android startActivityForResult Tutorial
These are the code segments in sequence:
ViewNote
menuIntent = new Intent(this, EditNote.class);
menuIntent.putExtra("NoteID", nNoteID);
menuIntent.putExtra("NoteDetails", new ArrayList<>(noteDetails));
if(noteFiles.size() > 0)
menuIntent.putParcelableArrayListExtra("NoteFiles", new ArrayList<>(noteFiles));
startActivityForResult(menuIntent, REQUEST_CODE); // REQUEST_CODE = 1
EditNote
Intent u = new Intent();
u.putExtra("ID", vNoteID);
u.putExtra("Type", ty);
u.putExtra("Summary", su);
u.putExtra("Source", so);
u.putExtra("Authors",au);
setResult(RESULT_OK,u);
finish();
Back to ViewNote onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE){
if(resultCode == RESULT_OK) {
nNoteID = data.getIntExtra("ID", 0);
nType = data.getStringExtra("Type");
nSummary = data.getStringExtra("Summary");
nSource = data.getStringExtra("Source");
nAuthors = data.getStringExtra("Authors");
}
}
}
Answered By - svstackoverflow
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.