save the data using shared preference in android

南笙酒味 提交于 2019-12-06 03:30:44

Put it into the starting intent:

Intent it = new Intent(Main1.this,Main2.class);
it.putExtra("MY_STRING_ARRAY", s2);

Get it back in the second activity:

String[] myStringArray = getIntent().getStringArrayExtra("MY_STRING_ARRAY");

If you want to send data from one activity to another then the best way would be send data using Intent object's putExtra method

Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("data1", "some data");
i.putExtra("data2", "another data");
i.putExtra("data3", "more data");
startActivity(i);

and you can get the data from receiving activity Activity2 like this

Object data1 = getIntent().getExtras().get("data1");

Hope that helps

Balibrera

If you want to save your information via SharedPreference not just pass it along activities, use some code like this:

SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
    SharedPreferences.Editor prefEditor = settings.edit();
    prefEditor.putString("string_preference", "some_string");
    prefEditor.putInt("int_preference", 18);
    prefEditor.commit(); 

The commit command is the responsable of actually saving data to SharedPreferences.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!