It's a complement to this question.
I can launch the Activity but I also need to be able to get the result. How do I do it?
I tried overriding onActivityResult on my PreferencesActivity to no avail.
Am I missing some extra properties in the preferences.xml?
If you want to pass data from the preference activity to your main activity use this code:
In your main activity class (launch):
In your preference activity class (set the result):
In your main activity class (get the result):
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
In your preferences activity: recieve the data attached to the intent
[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
Try overriding
startActivity()
in your PreferencesActivity class and make it callstartActivityForResult()
instead after checking that the intent is the one we are interested in, similar to the following (with 1234 as the request code):Depending on your needs, this could be simpler compared to adding several
OnPreferenceClickListener
in your code :)If you take a look at PreferenceActivity.java in platform source code here at line 1000 you can see that your intent is called via startActivity
startActivity(header.intent);
and not via startActivityForResult, so I don't think this is possible.However you could try to override the
onHeaderClick
function along with theonActivityResult
of the PreferenceActivity and see what happens. I didn't try it myself so I don't know and there is a good chance that this approach will be broken in future versions.But maybe there is another approach that could work for you. As I can see from your reference question you are launching an activity via an intent. If this activity is for settings editing then this is NOT the correct approach since android uses this intent just for launching an activity and nothing more. In my opinion it is better to create your specific preference activity by extending any of the available ones in order to customize it. Here is my customized ListPreference that I use in order to let the user select an application:
And i am using it in my preferences.xml like this:
By using this approach I can control everything my user does in this activity without breaking android's rules about preferences.
Hope this helps.
The cleanest solution that I know of is to listen to the click on the preference and launch the intent explicitly. This way
onActivityResult
will be called as usual.Assuming that your intent-preference is defined in XML you can attach a listener to it like this (where
1234
is a request code foronActivityResult
):