簡體   English   中英

有沒有一種方法可以在活動之間不使用意圖發送數據?

[英]Is there a way to send data between activities without using intent?

對於要發送給某個活動的每個用戶,我都有一個唯一的用戶名,但是我不想使用意圖:

    //create an intent and sends username
     Intent intent = new Intent(RegisterOwner.this, Owner.class);
     intent.putExtra("USERNAME",usernam);
     startActivity(intent);

我想將數據發送到活動而無需轉到該活動, startActivity(intent); 讓我去參加活動; 我不要

我只想發送數據而不啟動其他活動。

我想您希望活動之間可以使用特定的數據。 實現此目的的方法之一是利用SharedPreferences。

從您的第一個活動(RegisterOwner)

SharedPreferences mySharedPreferences = this.getSharedPreferences("MYPREFERENCENAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("USERNAME",usernam);
editor.apply();

完成此操作后,用戶名將存儲在共享首選項中。 現在,您可以在整個應用程序中使用這些數據。

因此,您可以從“所有者”活動中檢索以下內容:

SharedPreferences mySharedPreferences = this.getSharedPreferences("MYPREFERENCENAME", Context.MODE_PRIVATE);
String username = mySharedPreferences.getString("USERNAME", "");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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