android 活动传递数据,如何在Android应用程序的“活动”之间传递数据?
我有一種情況,在通過登錄頁面登錄后,每個activity上都會有一個退出button 。
點擊sign-out ,我將傳遞已登錄用戶的session id以便退出。 誰能指導(dǎo)我如何使session id可供所有activities ?
這種情況的任何替代方法
#1樓
在活動之間傳遞數(shù)據(jù)的最方便方法是傳遞意圖。 在您要發(fā)送數(shù)據(jù)的第一個活動中,應(yīng)添加代碼,
String str = "My Data"; //Data you want to send
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("name",str); //Here you will add the data into intent to pass bw activites
v.getContext().startActivity(intent);
您還應(yīng)該導(dǎo)入
import android.content.Intent;
然后,在下一個Acitvity(SecondActivity)中,應(yīng)該使用以下代碼從意圖中檢索數(shù)據(jù)。
String name = this.getIntent().getStringExtra("name");
#2樓
另一種方法是使用存儲數(shù)據(jù)的公共靜態(tài)字段,即:
public class MyActivity extends Activity {
public static String SharedString;
public static SomeObject SharedObject;
//...
#3樓
活動之間的數(shù)據(jù)傳遞主要是通過意圖對象進行的。
首先,您必須使用Bundle類將數(shù)據(jù)附加到意圖對象。 然后使用startActivity()或startActivityForResult()方法調(diào)用活動。
您可以通過博客文章“將
#4樓
我最近發(fā)布了Vapor API ,這是一個jQuery風(fēng)格的Android框架,它使諸如此類的各種任務(wù)變得更加簡單。 如前所述, SharedPreferences是您可以執(zhí)行此操作的一種方法。
VaporSharedPreferences被實現(xiàn)為Singleton,因此是一種選擇,并且在Vapor API中,它具有重載的.put(...)方法,因此您不必明確擔(dān)心要提交的數(shù)據(jù)類型-只要受支持即可。 它也很流利,因此您可以鏈接呼叫:
$.prefs(...).put("val1", 123).put("val2", "Hello World!").put("something", 3.34);
它還可以選擇自動保存更改,并在后臺統(tǒng)一讀取和寫入過程,因此您無需像在標(biāo)準Android中一樣顯式地檢索Editor。
或者,您可以使用Intent 。 在蒸氣API,您還可以使用可鏈接的重載.put(...)上的方法VaporIntent :
$.Intent().put("data", "myData").put("more", 568)...
如其他答案中所述,并將其作為額外的內(nèi)容傳遞。 您可以從Activity檢索其他內(nèi)容,此外,如果您使用的是VaporActivity此操作會自動完成,因此您可以使用:
this.extras()
要在“ Activity的另一端檢索它們,請切換到。
希望一些人感興趣:)
#5樓
您只需要在發(fā)送意向時發(fā)送額外內(nèi)容。
像這樣:
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Variable name", "Value you want to pass");
startActivity(intent);
現(xiàn)在,在SecondActivity的OnCreate方法上,您可以像這樣獲取其他功能。
如果您發(fā)送的值long :
long value = getIntent().getLongExtra("Variable name which you sent as an extra", defaultValue(you can give it anything));
如果您發(fā)送的值為String :
String value = getIntent().getStringExtra("Variable name which you sent as an extra");
如果您發(fā)送的值是Boolean :
Boolean value = getIntent().getBooleanExtra("Variable name which you sent as an extra", defaultValue);
總結(jié)
以上是生活随笔為你收集整理的android 活动传递数据,如何在Android应用程序的“活动”之间传递数据?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android cordova 教程,c
- 下一篇: oracle active data g