我的
SetListEditAdapter
类中的这个 Intent 启动了我的父类 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);
}
在新打开的
parent
活动 (SetListMasterViewActivity
) 中,我启动子活动 (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);
我在
child
活动 (SetListOpenViewActivity
) 中毫无问题地收到意图:
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");
当按下
child
活动中的“返回”按钮时,处理将返回到 parent
活动,并具有以下意图:
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();
在
parent
活动中,我希望收到 child's
意图:
@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
// ...
}
}
}
但是 Log.d 输出引用正在启动
parent
活动的 Intent:
D (SetListMasterViewActivity) - line #257 - Intent received: Bundle[{SET_LIST_ID=280}]
我已经尝试过:
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) {
...
然而,所有
Log.d
语句均未达成。
child activity
: @Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
但是,在
parent
中,没有收到任何 child's
Intents
。
根据文档方法Activity.getIntent()
返回启动此活动的意图。
通过调用
finish()
,您并不是有意启动一个新活动(就像 startActivity(intent)
那样。它只是销毁该活动并从后堆栈中弹出前一个活动。
为了从子活动返回结果意图,您需要使用
startActivityForResult()
启动它并在 onActivityResult()
中等待数据。但当Activity Result API
出现后,现代的方式发生了一些变化。从指南开始。