简体   繁体   English

如何使用SharedPreferences将数据从活动传递到另一个活动

[英]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_idusernameusernamenameemail并分配了值):

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.

相关问题 如何将sharedpreferences从一个活动传递到另一个活动? - How to pass sharedpreferences from one activity to another activity? 如何将数据从活动传递到另一个活动,然后再传递到Android上的另一个活动? - How to pass data from an activity to another activity and then to another activity on Android? 如何使用存储在非活动类的sharedpreferences中的数据 - How to use data stored in sharedpreferences from non-activity class 如何从一个活动的列表视图传递数据并将其传递给另一活动? - How to pass the data from listview of one activity and pass it to another activity? 如何将SharedPreferences从一个活动发送到另一个活动 - How to send SharedPreferences from one Activity to another 如何将数据从一个活动传递到另一活动 - How to pass data from one activity to another activity 如何将数据从一个活动传递到 Android 中的另一个活动 - How to pass data from one activity to another activity in Android 如何将图像数据从一个活动传递到另一个活动? - How to pass image data from one activity to another activity? 我如何将数据从一个活动传递到另一活动 - how i pass data from one activity to another activity 如何将数据从活动传递到Android上的另一个活动 - How to pass data from activity to another activity on Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM