[英]how to use SharedPreferences to pass data from activity to another activity
i'm trying to pass my data from first activity to another activity say it fourth activity. 我正在尝试将我的数据从第一个活动传递到另一个活动,即第四个活动。 i'm using the code on
onClick
for button in first activity where the data comes from and also in onclick
button in fourth activity when the data want to pass from first activity. 我在数据来自的第一个活动中使用
onClick
for button上的代码,而当数据要从第一个活动传递时,我也在第四个活动中的onclick
按钮中使用代码。 but it wont work. 但它行不通。 it just pass the 0 data which is i set it for default.
它只是传递0数据,这是我默认设置的数据。 please help
请帮忙
this is SharedPreferences
code in my first activity 这是我的第一个活动中的
SharedPreferences
代码
SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("user_id", user_id);
editor.putString("username", usernameJ);
editor.putString("name", name);
editor.putString("email", email);
editor.apply();
and this is SharedPreferences
code in my four activity: 这是我的四个活动中的
SharedPreferences
代码:
SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
user_id = sharedPreferences.getInt("user_id", 0);
new UbahLapor().execute();
editor.apply()
does the work in a thread (asynchronous), So it might take time. editor.apply()
在线程(异步)中完成工作,因此可能需要时间。
so try 所以尝试
editor.apply();
instance of 的实例
editor.commit;
Save your preferences this way (provided that user_id
, username
, name
and email
have already been declared and assigned values): 以这种方式保存您的首选项(前提是已经声明了
user_id
, username
, username
, name
和email
并分配了值):
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putInt("user_id", user_id);
editor.putString("username", usernameJ);
editor.putString("name", name);
editor.putString("email", email);
editor.apply();
and read it back: 并读回:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
user_id = sharedPreferences.getInt("user_id", 0);
You need to use editor.commit() instead of editor.apply() . 您需要使用editor.commit()而不是editor.apply() 。
apply() updates your sharedprefernces asynchronously. apply()异步更新您的共享首选项。 commit() updates your sharedprefernces synchronously.
commit()同步更新您的共享首选项。 Note that commit() is a blocking call.
请注意,commit()是阻塞调用。
Ty this 这个
First if you store data using shared preference then try: 首先,如果您使用共享首选项存储数据,请尝试:
SharedPreferences.Editor editor =
PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putString("username", username);
editor.putString("name", name);
editor.apply();
get data using shared preference 使用共享首选项获取数据
SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
username= sharedPreferences.getString("username", null);
Second you pass the data first activity to another activity using intent then try: 其次,您使用意图将数据优先活动传递给另一个活动,然后尝试:
Intent intent = new Intent(activity, activityClass);
intent.putExtra("message", text);
activity.startActivity(intent);
get the data using intent 使用意图获取数据
Intent intent=getIntent();
success_msg_txt.setText(intent.getStringExtra("message"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.