-2

This is the write activity. In here I want to write a text and after clicking a button I want this text to appear in a new activity called read activity.

public class Write extends Activity implements OnClickListener
{
    EditText text; TextView retrive1;
    public static String filename="Mysharedstring" ;
    SharedPreferences someData;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.write);
        setupVariables();
        someData = getSharedPreferences(filename, 0);
    }

    private void setupVariables()
    {
        Button sav= (Button) findViewById(R.id.save);
        Button ret= (Button) findViewById(R.id.retrive);
        text= (EditText) findViewById(R.id.txtText);
        retrive1= (TextView) findViewById(R.id.textview);
        ret.setOnClickListener(this);
        sav.setOnClickListener(this);
    }

    public void onClick(View v)
    {
        String stringdata= text.getText().toString();
        SharedPreferences.Editor editor = someData.edit();
        editor.putString("sharedString", stringdata);
        editor.commit();
    }
}

I don't know what to write in the read activity.

public void onClick(View v)
{
    //???
}
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
Mowi
  • 21
  • 1
  • 3
  • Did you check the Android documentation? You can check here (http://developer.android.com/training/basics/firstapp/starting-activity.html) and here (http://developer.android.com/guide/components/intents-filters.html) that is your first step. – prmottajr Dec 10 '13 at 22:10
  • Check out this question http://stackoverflow.com/questions/4878159/android-whats-the-best-way-to-share-data-between-activities – Sipka Dec 10 '13 at 22:10

2 Answers2

1

The most simple way that I can think of is this first Activity

Intent intent= new Intent(this, theOtherActivity.class);
intent.putExtra(Key, "Value");
startActivity(intent);   

And on theOtherActivity

String receivedData=getIntent().getExtras().getKey(Key);
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
George Daramouskas
  • 3,720
  • 3
  • 22
  • 51
0
public void onClick(View v)
{
   // get the shared string from the SharedPreference
   SharedPreferences sp = getSharedPreferences("Mysharedstring",0);
   String s = sp.getString("sharedString","Nothing found.");

   // display the shared string
   Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}

Sources for SharedPreferences:

How to save app settings?

How to keep information about an app in Android?

Shared Preferences | Android Developers

Community
  • 1
  • 1
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97