-1

First Activity:

public class MainActivity extends Activity {

    private Button btn;
    private Button btnsbm;
    private EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.button);
        btnsbm=(Button)findViewById(R.id.button2);
        et=(EditText)findViewById(R.id.editText);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("prefData", MODE_PRIVATE);
                SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putString("key",et.getText().toString());
                editor.apply();
                Toast.makeText(MainActivity.this, "Data added",Toast.LENGTH_LONG).show();
            }
        });
        gotonextactivity();
    }

    public void gotonextactivity() {
        btnsbm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Activty_2.class);
                startActivity(intent);
            }
        });
    }
}
Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
Rahul
  • 69
  • 8

4 Answers4

0

While this question is answered.I would like to advise you that using SharedPreferences is not the way to transfer data between activities.

From the documentation

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

If your requirements are such.Then you could explain your question a bit more for why you chose this approach.

A common practice amongst all android developers is to use extras in intents (check the section under extras).

you can use putExtras() to pass data to the next activity and retrieve them as explained in this SO answer

Community
  • 1
  • 1
Droidekas
  • 3,464
  • 2
  • 26
  • 40
0

There is no need for a Sharedpreference to do that job, you can just put your data inside an Intent and get them inside the new activity. First edit your button's onClick() to be like this

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this,"Data added",Toast.LENGTH_LONG).show();

        Intent intent = new Intent(MainActivity.this,Activty_2.class);
        intent.putExtras("data", et.getText().toString());
        startActivity(intent);
});

then inside your Activty_2 onCreate() method you add

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("data"); // this is your text
}

hope it helps.

SaNtoRiaN
  • 2,212
  • 2
  • 15
  • 25
0

You are making it difficult my friend, you just need to use intent to pass data to your other activity.

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this,SecondActivity.class);
        intent.putString("key",editText1.gettext().toString());
        startActivity(intent);
    }
});

Now get that data from you intent in SecondActivity in onCreate():

Intent intent = getIntent();
String data = intent.getString("key");
textView.setText(data);
Darshan
  • 515
  • 1
  • 3
  • 16
-1

write editor.commit(); not editor.apply(); . and please give proper error. Sharedpreference not filling or problem is something else?

akhil batlawala
  • 1,066
  • 1
  • 10
  • 30