how to use SharedPreferences to pass data from activity to another activity

android get sharedpreferences from another activity
how to use shared preference in android between activities
how to pass data from one application to another in android
shared preference login example in android
how to get data from shared preference in android
get value from shared preference in android
sharedpreferences in android programmatically
how to create sharedpreferences class in android

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. but it wont work. it just pass the 0 data which is i set it for default. please help

this is SharedPreferences code in my first activity

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 = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
user_id = sharedPreferences.getInt("user_id", 0);
new UbahLapor().execute();

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"));

How to Pass Data from One Activity to Another in Android, How do I transfer data from one activity to another activity? This example demonstrate about How to pass data from one activity to another in Android using shared preferences. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

editor.apply() does the work in a thread (asynchronous), So it might take time.

so try

 editor.apply();

instance of

 editor.commit;

Sending data to other activity using sharedpreferences android , is passed in key value pair. The value can be of types like int, float, long, string, etc. Method 1: Using Intent We can send data while calling one activity from another activity using intent. All we have to do is add the data to Intent object using putExtra() method. The data is passed in key value pair.

Save your preferences this way (provided that user_id, username, name and email have already been declared and assigned values):

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);

Handling Data between Activities in Android using Shared , How do I send a text from one activity to another activity? Lets say you have stored the value in sharedPreferences. Now you want to go from MainActivity.java to NextActivity.java (Just an example). In NextActivity.java class, you need to get the sharedPreferences in such a way:

You need to use editor.commit() instead of editor.apply().

apply() updates your sharedprefernces asynchronously. commit() updates your sharedprefernces synchronously. Note that commit() is a blocking call.

How To Pass Data Between Activities #2 (SharedPreference , How pass Textview value from one activity to another activity in Android? Passing data in Android using sharedPreferences For sharing simple data types between Activities or services we can make use or sharedPreferences , in android. SharedPreferences or preferences are useful for simple dataTypes, where they are stored as Key/Values pairs in a xml file.

Android Beginner Tutorial #20, for sending data for new activity Declare both variable in main activity public static final String TAG_NAME = "nameKey"; public static final String  Following is a simple example how to pass data from one activity to another activity in Android. As bonus, we'll track how much user click on button (methods onSaveInstanceState() and onRestoreInstanceState()) and save state of main activity between starts (SharedPreferences). The main object for message passing is intent object. As a message object, it's purpose is to communicate with other components of the application.

Passing data and saving values using Shared Prefrence, After installation, create a new Android studio project. In this tutorial, we will be needing two Activities. One to input and save the data to shared preferences and​  To retrieve the data in some other activity you need to re-declare the SharedPreferences object with the same filename and mode. Here I have made this example where user selects his gender and Android version from the Spinners and clicks the Button.

Passing data from activity to activity using (Shared Preference ), Open another Activity or Service from the current Activity 2. Pass How To Add Background Duration: 9:46 Posted: Oct 9, 2017

Comments
  • Your code looks (basically) fine to me. Are you certain that those variables have values when you assign them to shared preferences in your first activity?