The Preference class allows for an Intent to be set, to have a preference activate another activity when clicked, but I'm unable to find a way to handle the resul开发者_运维百科t from an activity using this method. There's also the DialogPreference where I can provide a custom view, but I don't have direct access to the view I want to use, only an activity.
Digging a bit further, it looks like the RingtonePreference uses a few package internal methods on PreferenceManager to receive results from a started sub-activity, but as these are package internal I am unable to do the same.
Is there any other way of handling a custom preference with an activity that returns a result (where the result is to be saved as the value of the preference)?
I have also noticed PreferenceActivity does not return onActivityResult. That being said, is there a reason your SubActivity couldn't save the preference directly? If you need to check the value of it, you could check it at onResume of your PreferenceActivity as a workaround..
//SubActivity onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences prefs = getSharedPreferences(TestPreferenceActivity.PREFS_FILE, MODE_WORLD_READABLE);
prefs.edit().putString("mykey", "someValue").commit();
finish();
}});
}
//PreferenceActivity onResume
@Override
protected void onResume() {
Log.d(TAG, "Preferences Resumed");
//Check for new Preference Values
SharedPreferences prefs = getSharedPreferences(PREFS_FILE, MODE_WORLD_READABLE);
String value = prefs.getString("mykey", "defValue");
Log.d(TAG, "Current value is: " + value);
super.onResume();
}
精彩评论