[英]Return more than One Intent from Second Activity to Main Aactivity
我在MainActivity
使用onActivityResult
,我想將SecondActivity
數據發送回MainActivity
但是在SecondActivity
我有兩個具有兩個不同webservice
不同功能,我該怎么做。
這是在MainActivity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
// form Theaudiodb.com
Glide.with(this).load(data.getStringExtra("artistThumb")).into(headerImageView);
//from onemusic.com
biographyAliases.setText(data.getStringExtra("aliases"));
}
}
這是SecondActivity
void parsAndSendTheAudiDbBiography(String response) {
try {
Gson gson = new Gson();
TheaudiodbBiography theaudiodbBiography = gson.fromJson(response, TheaudiodbBiography.class);
returnIntent.putExtra("artistThumb", checkNullable(theaudiodbBiography.getArtists()
.get(0).getStrArtistThumb()));
setResult(Activity.RESULT_OK, returnIntent);
finish();
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
void parseAndShowOneMusicBio(String response) {
try {
Gson gson = new Gson();
OneMusicBiography[] oneMusicBio = gson.fromJson(response, OneMusicBiography[].class);
Intent returnIntent= new Intent(DialogActivity.this, MainActivity.class);
returnIntent.putExtra("aliases", oneMusicBio[0].getAliases().toString());
setResult(Activity.RESULT_OK,returnIntent);
finish();
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
你不可以做這個。
但是您可以將semafor放入巡回第二活動中,並在2個通話結束后開始意圖進入第一個活動,並且可以在其中添加2個附加功能。
或者,您可以/應該將結果存儲在數據庫中,並發送記錄ID到第一活動數據庫。
像這樣的東西:
//we can set it to 2 as we know that there are 2 calls
private int semafor = 2;
private Intent returnIntent= new Intent(DialogActivity.this, MainActivity.class);
void parsAndSendTheAudiDbBiography(String response) {
try {
Gson gson = new Gson();
TheaudiodbBiography theaudiodbBiography = gson.fromJson(response, TheaudiodbBiography.class);
returnIntent.putExtra("artistThumb", checkNullable(theaudiodbBiography.getArtists()
.get(0).getStrArtistThumb()));
semafor --;
if (semafor == 0) {
finish();
}
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
void parseAndShowOneMusicBio(String response) {
try {
Gson gson = new Gson();
OneMusicBiography[] oneMusicBio = gson.fromJson(response, OneMusicBiography[].class);
returnIntent.putExtra("aliases", oneMusicBio[0].getAliases().toString());
semafor --;
if (semafor == 0) {
finish();
}
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.