If you want to pass data from the preference activity to your main activity use this code:
In your main activity class (launch):
startActivityForResult(new Intent(main.this, preferences.class), 0);
In your preference activity class (set the result):
Intent i;
i.putStringExtra("name", "tom");
setResult(RESULT_OK, i);
finish();
In your main activity class (get the result):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK){
Log.d("test", data.getExtraString("name");
}
}
}
You can put as many extras as you want and not only strings but all standard data types.
Hope i did everything right ;)
EDIT
As Kaarel told me, I probably missunderstood the question.
This is how you can recieve data from the main activiy in the preferences activity:
In your main activity: launch the preferences activity and attach the data
String foo = "hello";
Intent i = new Intent();
i.putExtra("testString", foo);//You can also add other types of variables here, see [1] for reference
i.setClass(main.this, preferences.class);
startActivity(i);
In your preferences activity: recieve the data attached to the intent
Bundle b = this.getIntent().getExtras();//[2]
if (b!=null){
String recievedString = b.getString("testString");
//The recievedString variable contains the String "hello" now, see [3]
}
[1] https://developer.android.com/reference/android/content/Intent.html
[2] https://developer.android.com/reference/android/content/Intent.html#getExtras%28%29
[3] https://developer.android.com/reference/android/os/Bundle.html