如何将数据从多个活动传递到一个活动



我有5个活动:*活动一(一场比赛详情)*活动2(另一场比赛的细节)等....*活动5(付款活动)

在每个活动有一些数据与一个按钮,我想发送该数据支付活动每当用户点击任何活动(1-4)

使用Intent在活动之间发送数据

Intent intent = new Intent(context, Another.class);
intent.putExtra(“message_key”, message);
startActivity(intent);

然后使用这个代码来获取你的数据

Intent intent = getIntent();
String str = intent.getStringExtra(“message_key”);

快乐编码

或者你可以试试SharedPreferences

创建对象类

object Session {
private val NAME = "SOME_NAME"
private val MODE = Context.MODE_PRIVATE
private lateinit var preferences: SharedPreferences
fun init(context: Context) {
preferences = context.getSharedPreferences(NAME, MODE)
}
private inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
val editor = edit()
operation(editor)
editor.apply()
}
fun setString(tag: String, value: String) {
preferences.edit {
it.putString(tag, value)
}
}
fun getString(tag: String) = preferences.getString(tag, "")
}

init that in Application inside onCreate

class MyApp: Application(){
override fun onCreate() {
super.onCreate()
Session.init(this)
}
}

最新更新