I am a new developer in android applications.i would like to save data using a common concept of preferences. I save the data in one action and get the same data in another activity.here, I would like to send String a [] = {"one", "two", "three"} one kind of activity to another. I wrote the code as follows
Main1.java
public class Main1 extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences shp=getSharedPreferences("TEXT", 0);
final Editor et=shp.edit();
((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String s1=((EditText)findViewById(R.id.editText1)).getText().toString();
et.putString("DATA", s1);
String s2[]={"one","two","three"};
et.commit();
Intent it=new Intent(Main1.this,Main2.class);
startActivity(it);
}
});
}
Main2.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
String kk=getSharedPreferences("TEXT", 0).getString("DATA", null);
((EditText)findViewById(R.id.editText1)).setText(kk);
}
can we get the values of a string array from Main1.java to Main2.java?
source
share