How to pass sharedpreferences from one activity to another activity?

how to pass data from one activity to another in android using sharedpreferences
how to get sharedpreferences value in another activity kotlin
how to use shared preference in android between activities
how to pass data from one application to another in android
how to get data from shared preference in android
sharedpreferences android
shared preference login example in android
how to store username and password in sharedpreferences in android

I have two activities in my project one activity is MainActivity and another is Main2activity, In Main2activity I'm taking input from the user and storing it in SharedPreference, Now I want to pass this data to MainActivity and display that data to the user.

The code for Main2activity is

 package com.example.to_doapp;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import java.io.Serializable;
import java.util.ArrayList;
public class Main2Activity extends AppCompatActivity {




public void BackMain ( View view )
{
    Intent intent = new Intent( getApplicationContext() ,MainActivity.class ) ;
    SharedPreferences sharedPreferences = this.getSharedPreferences( "com.example.to_doapp;", Context.MODE_PRIVATE);
    EditText editText = ( EditText) findViewById( R.id.editText3) ;
    String s = editText.getText().toString();
    sharedPreferences.edit().putString("task" , s ) .apply();

    //intent.putStringArrayListExtra("key",arr);
    startActivity(intent);



}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

}

code foe Mainactivity is

package com.example.to_doapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


public void onclick (View view){

    Intent intent = new Intent(getApplicationContext(), Main2Activity.class );
    startActivity(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    Intent intent = getIntent();


 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

I want to know how to pass sharedpreferences and how to display that data to user in listview. Thank you for help in advance.

You can use SharedPreferences getString() method in your second activity. Here is the documentation: https://developer.android.com/reference/android/content/SharedPreferences.html#getString(java.lang.String,%20java.lang.String)

How to pass data from one activity to another in Android using , This example demonstrate about How to pass data from one activity to another in Android using shared preferences. Step 1 − Create a new� use getApplicationContext() instead of this in both Activities as:. In activity A the SharedPreferences in following way: private void SavePreferences(String key, String value){ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(key, value); editor.commit

SharedPreferences sharedPreferences = this.getSharedPreferences( "com.example.to_doapp;", Context.MODE_PRIVATE);
sharedPreferences.getString("task", "");

You can use the above code to get the data from shared preference any time until it is cleared. But i would suggest you create a class for the same and carry out all the Preference related task in the same

How To Pass Data Between Activities #2 (SharedPreference , It shows also how to change the text of a TextView into the data of the variable that was passed Duration: 9:46 Posted: Oct 9, 2017 How to Pass Data from One Activity to Another in Android 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 simplest way is:

Set SharedPref:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
Editor editor = prefs.edit();
editor.putString(PREF_NAME, "someValue");
editor.commit();

Get SharedPref:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
String yourValue = prefs.getString(PREF_NAME, "");

Android Beginner Tutorial #20, Android Beginner Tutorial #20 - Shared Preferences [Passing Data Between Activities] In Duration: 5:18 Posted: Mar 30, 2017 This example demonstrates how do I pass an object from one Activity to another in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

No need to pass SharedPreferences . you get any activity or fragment on SharedPreferences value..

SharedPreferences shared = getSharedPreferences("Your SharedPreferences name", MODE_PRIVATE);
String data= shared.getString("key", "");

Note : key should be same as edit or save data time used. also SharedPreferences name same.

Passing data and saving values using Shared Prefrence, Tutorial will tell about how to pass data from one activity to another activity and store simple Duration: 7:24 Posted: Nov 11, 2017 When the button is clicked, is calls getResults (View v). This method grabs all the values from the EditText fields and adds them to SharedPreferences then calls “Results” activity. If the values have already been set then it sets the EditText fields to those values. I trimmed the code so this is not a complete app, but it will run.

When you add a shared preference for the app, it can be accessed from anywhere within the app.

SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(getContext());
String data= shared.getString("nameOfValue", "");

how to retrieve a stored SharedPreference value from mainActivity to , the input value from our edittext from main activity. But how do we retrieve the stored shared preference in a different activity? help I'm stuck� 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.

Handling Data between Activities in Android using Shared , 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 � The easy way is like this (Works in any activity): Save to preferences SharedPreferences preferences = PreferenceManager . getDefaultSharedPrefererences ( this ); SharedPreferences .

Android, how to pass data from one activity to another in android using sharedpreferences sharedpreferences - android how to get sharedpreferences value in another� Android Beginner Tutorial #20 - Shared Preferences [Passing Data Between Activities] In this video we show you how to pass data between activities using SharedPreferences. Source Code: ↻ https

Android Shared Preferences Example Tutorial, Shared Preferences allows activities and applications to keep preferences, SharedPreferences is application specific, i.e. the data is lost on performing one of the following options: Activity (or other application Context), to access application-level preferences Pass an integer instead of null in the second argument. Android is one of the most popular operating systems for mobiles. I am sure all the chat applications use this function to transfer the message user activity to another user activity. I will show you how to send the data one activity to another activity in an android application using the Android studio. Android is the kernel-based operating

Comments
  • No need to pass SharedPreference from one screen to other because You can access SharedPreference object from anywhere in the app by keys.
  • @RutvikBhatt please elaborate !! As I'm newbie I'm unable to understand.
  • Possible duplicate of Android Shared preferences example
  • @swati I'm not able to understand !!
  • Shahnavaz , You mean to say that I add this code to my MainActivity to get the access of the data stored in sharedpreferences. After this how do I display this data to user !!
  • Yes and depends on how do you want to display it. You can use a Toast.makeText(this,"This is your data" + task,Toast.LENGTH_SHORT).show(); or you could use a TextView to display it link
  • what if our sharedpreferences contain more than one data, then what will be the string in yourValue ? Basically, I want to know how prefs.getString(PREF_NAME, "") works
  • How do I display the data stored in it to user please specify !!
  • you can display any view control.
  • where do I add this class sharedpreferences and how do I display it to user?
  • @shalininegi I think you are new in Android please make this class in your main package