簡體   English   中英

獲取一項活動發送給所有活動的數據

[英]Get data send by one activity to all activities

我有活動A,它使用Intent調用活動B,並將一些數據放入intent中,現在我想訪問活動C中A發送的數據,我可以這樣做嗎?

簡單使用這段代碼

String value = getIntent().getExtras().getString("keyName");


或者你可以使用SharedPreferenceActivityA然后檢索ActivityC
或者,你可以使用POJO (即gettersetter )設置在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM