日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 活动传递数据,如何在Android应用程序的“活动”之间传递数据?

發(fā)布時(shí)間:2024/9/27 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 活动传递数据,如何在Android应用程序的“活动”之间传递数据? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我有一種情況,在通過登錄頁面登錄后,每個(gè)activity上都會(huì)有一個(gè)退出button 。

點(diǎn)擊sign-out ,我將傳遞已登錄用戶的session id以便退出。 誰能指導(dǎo)我如何使session id可供所有activities ?

這種情況的任何替代方法

#1樓

在活動(dòng)之間傳遞數(shù)據(jù)的最方便方法是傳遞意圖。 在您要發(fā)送數(shù)據(jù)的第一個(gè)活動(dòng)中,應(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;

然后,在下一個(gè)Acitvity(SecondActivity)中,應(yīng)該使用以下代碼從意圖中檢索數(shù)據(jù)。

String name = this.getIntent().getStringExtra("name");

#2樓

另一種方法是使用存儲(chǔ)數(shù)據(jù)的公共靜態(tài)字段,即:

public class MyActivity extends Activity {

public static String SharedString;

public static SomeObject SharedObject;

//...

#3樓

活動(dòng)之間的數(shù)據(jù)傳遞主要是通過意圖對(duì)象進(jìn)行的。

首先,您必須使用Bundle類將數(shù)據(jù)附加到意圖對(duì)象。 然后使用startActivity()或startActivityForResult()方法調(diào)用活動(dòng)。

您可以通過博客文章“將

#4樓

我最近發(fā)布了Vapor API ,這是一個(gè)jQuery風(fēng)格的Android框架,它使諸如此類的各種任務(wù)變得更加簡(jiǎn)單。 如前所述, SharedPreferences是您可以執(zhí)行此操作的一種方法。

VaporSharedPreferences被實(shí)現(xiàn)為Singleton,因此是一種選擇,并且在Vapor API中,它具有重載的.put(...)方法,因此您不必明確擔(dān)心要提交的數(shù)據(jù)類型-只要受支持即可。 它也很流利,因此您可以鏈接呼叫:

$.prefs(...).put("val1", 123).put("val2", "Hello World!").put("something", 3.34);

它還可以選擇自動(dòng)保存更改,并在后臺(tái)統(tǒng)一讀取和寫入過程,因此您無需像在標(biāo)準(zhǔn)Android中一樣顯式地檢索Editor。

或者,您可以使用Intent 。 在蒸氣API,您還可以使用可鏈接的重載.put(...)上的方法VaporIntent :

$.Intent().put("data", "myData").put("more", 568)...

如其他答案中所述,并將其作為額外的內(nèi)容傳遞。 您可以從Activity檢索其他內(nèi)容,此外,如果您使用的是VaporActivity此操作會(huì)自動(dòng)完成,因此您可以使用:

this.extras()

要在“ Activity的另一端檢索它們,請(qǐng)切換到。

希望一些人感興趣:)

#5樓

您只需要在發(fā)送意向時(shí)發(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应用程序的“活动”之间传递数据?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。