开发者

How do I create a custom preference that uses an existing activity that returns a result?

开发者 https://www.devze.com 2023-01-29 12:41 出处:网络
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 usi

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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消