[英]Get data send by one activity to all activities
我有活動A,它使用Intent調用活動B,並將一些數據放入intent中,現在我想訪問活動C中A發送的數據,我可以這樣做嗎?
String value = getIntent().getExtras().getString("keyName");
或者你可以使用SharedPreference
在ActivityA
然后檢索ActivityC
或者,你可以使用POJO
(即getter
和setter
)設置在ActivityA
然后獲取ActivityC
例如
public class Example{
private String name;
private int age;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
像這樣將數據放入ActivityA
Intent intent = new Intent(this, ClassB.class);
intent.putExtra("yourKey", yourValue);
startActivity(intent);
使用此方法從ActivityB獲取您的值
Intent intentA = getIntent();
String yourString = intentA.getStringExtra("yourKey");
希望這可以幫助:)
為所有活動創建基本活動,然后通過捆綁方法傳遞該活動中的日期。 現在,您可以在任何活動中獲取此數據。
您可以使用SharedPreferences保存值,並可以從任何活動中訪問它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.