[英]Resting Activity intent Values after press back and selecting another Button?
我試圖通過將食譜名稱,內容和圖像傳遞到其他活動中,方法是將它們放在其他“ PutStringExtra”中,並使用捆綁圖像。
第一次只能正常工作,但數據傳遞良好,但是當我單擊返回並單擊另一個配方時,它顯示與上一個相同! 所以我如何清除其他數據?
主要活動代碼:
final Intent intent = new Intent(getApplicationContext(),recipe.class);
//Recipe How to content
final String recipeA=getString(R.string.recipeA);
final String recipeB=getString(R.string.recipeB);
final String recipeD=getString(R.string.recipeD);
final String recipeAName=getString(R.string.recipeAName);
final String recipeBName=getString(R.string.recipeBName);
final String recipeDName=getString(R.string.recipeDName);
final Bundle bundle = new Bundle();
public void onItemClick(View view, int i) {
switch (i) {
case 0:
intent.putExtra("recipeA",recipeA);
intent.putExtra("recipeAName",recipeAName);
bundle.putInt("imageA",R.drawable.a);
intent.putExtras(bundle);
startActivity(intent);
break;
case 1:
intent.putExtra("recipeB",recipeB);
intent.putExtra("recipeBName",recipeBName);
bundle.putInt("imageB",R.drawable.b);
intent.putExtras(bundle);
startActivity(intent);
break;
case 2:
intent.putExtra("recipeD",recipeD);
intent.putExtra("recipeDName",recipeDName);
bundle.putInt("imageD",R.drawable.d);
intent.putExtras(bundle);
startActivity(intent);
break;
第二活動代碼:
intent= getIntent();
Bundle bundle = this.getIntent().getExtras();
if (intent.hasExtra("recipeA")) {
String recipeA = intent.getStringExtra("recipeA");
String recipeAName = intent.getStringExtra("recipeAName");
recipeContent.setText(recipeA);
recipeName.setText(recipeAName);
int imageA= bundle.getInt("imageA");
mImageView.setImageResource(imageA);
intent.removeExtra("recipeA");
}
else if (intent.hasExtra("recipeB")) {
String recipeB = intent.getStringExtra("recipeB");
String recipeBName = intent.getStringExtra("recipeBName");
recipeContent.setText(recipeB);
recipeName.setText(recipeBName);
int imageB= bundle.getInt("imageB");
mImageView.setImageResource(imageB);
}
else if (intent.hasExtra("recipeD")) {
String recipeD = intent.getStringExtra("recipeD");
String recipeDName = intent.getStringExtra("recipeDName");
recipeContent.setText(recipeD);
recipeName.setText(recipeDName);
int imageD= bundle.getInt("imageD");
mImageView.setImageResource(imageD);
}
每次創建一個新的Intent。 沒有理由重復使用相同的Intent。
另外,請勿將添加額外內容的調用與添加到Intent的調用混合使用。 使用一個或另一個。 Intent.putExtraXXX將在其自己的Bundle上調用bundle.putExtraXXX。 設置捆綁包可能會清除您已經設置的那些東西。 要么在捆綁軟件中設置所有內容,然后添加捆綁軟件,要么在Intent上設置所有內容。 第二個比較正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.