This Intent from my SetListEditAdapter class launches my parent class SetListMasterViewActivity:
private void openSetListDetails(int setListId) {
if (openSetListMode == OpenSetListMode.OPEN) {
Intent openSetListIntent;
openSetListIntent = new Intent(context, SetListMasterViewActivity.class);
openSetListIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // Add the flag here
openSetListIntent.putExtra("SET_LIST_ID", setListId);
context.startActivity(openSetListIntent);
} else {
Intent intent;
intent = new Intent(context, SetListDetailsActivity.class);
intent.putExtra("SET_LIST_ID", setListId);
context.startActivity(intent);
}
Log.d("SttList", "(SetListEditAdapter) line #59 - setListId: " + setListId);
Log.d("SttList", "(SetListEditAdapter) line #61 - SetListMasterViewActivity launched with setListId: " + setListId);
}
In the newly opened parent activity (SetListMasterViewActivity) I launch the child activity (SetListOpenViewActivity):
Intent openViewIntent = new Intent(SetListMasterViewActivity.this, SetListOpenViewActivity.class);
// Pass necessary data with the intent
openViewIntent.putExtra("songId", selectedSong.getSongId());
openViewIntent.putExtra("songName", selectedSong.getSongName());
openViewIntent.putExtra("setId", setListId); // Assuming you need to pass the setListId
openViewIntent.putExtra("songsList", new ArrayList<>(songsList)); // Convert List to ArrayList before passing
// Start the activity
openViewIntent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(openViewIntent);
I receive the Intent in the child activity (SetListOpenViewActivity) without issue:
Intent intent = getIntent();
songId = intent.getLongExtra("songId", 0);
songName = intent.getStringExtra("songName");
setId = intent.getIntExtra("setId", 0);
dbHelper = new DatabaseHelper(this);
songsList = intent.getParcelableArrayListExtra("songsList");
When the "Back" button in the child activity is pressed, processing returns to the parent activity with this Intent:
Intent resultIntent = new Intent();
resultIntent.putExtra("lastViewedSongId", currentSongId);
resultIntent.putExtra("lastViewedSongName", currentSongName);
resultIntent.putExtra("backButtonClicked", true); // Include backButtonClicked flag
setResult(RESULT_OK, resultIntent);
Log.d("SttList", "(SetListOpenViewActivity) - line #120 - Intent contents: " + getIntent().getExtras());
Log.d("SttList", "(SetListOpenViewActivity) - line #121 - Setting result intent: " + resultIntent);
finish();
And in the parent activity I expect to receive the child's Intent here:
@Override
protected void onResume() {
super.onResume();
Log.d("SttList", "(SetListMasterViewActivity) - line #254 - onResume method start");
// Check if the Intent contains the backButtonClicked flag
Intent intent = getIntent();
Log.d("SttList", "(SetListMasterViewActivity) - line #257 - Intent received: " + getIntent().getExtras());
if (intent != null && intent.hasExtra("backButtonClicked")) {
boolean backButtonClicked = intent.getBooleanExtra("backButtonClicked", false);
Log.d("SttList", "(SetListMasterViewActivity) - line #260 - backButtonClicked: " + backButtonClicked);
if (backButtonClicked) {
// Handle the back button click
Log.d("SttList", "(SetListMasterViewActivity) - line #263 - Back button clicked in child activity");
// Process the backButtonClicked flag here or call onActivityResult if needed
// ...
}
}
}
However the Log.d output is referencing the Intent which is starting the parent activity:
D (SetListMasterViewActivity) - line #257 - Intent received: Bundle[{SET_LIST_ID=280}]
I have tried:
- Using
onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("SttList", "(SetListMasterViewActivity) - line #216 - Received intent: " + data);
Log.d("SttList", "(SetListMasterViewActivity) line #217 - requestCode: " + requestCode + ", resultCode: " + resultCode);
if (resultCode == RESULT_OK && data != null) {
...
However none of the Log.d statements are reached.
- tried adding this to the
child activity:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
but again, in the parent none of the child's Intents are received.
According docs the method Activity.getIntent()
With call
finish()you are not starting a new activity with intent (like dostartActivity(intent). It just destroys the activity and pops up the previous activity from the back stack.In order to return the result intent from the child activity you need to start it with
startActivityForResult()and wait data insideonActivityResult(). But the modern way changed a little whenActivity Result APIappeared. Start from guide.