It is expected that onUserInteraction
is being called for any user interaction. it works fine in PreferenceActivity
. However, when a DialogPreference
is popup, onUserInteraction
is not called anymore even there is user interaction such as touch event.
It seems that DialogPreference
is not the only case. Whenever Dialog
is shown, it does not report the user interaction to activity.
But what can I do if I really need it. Thank You.
As far as I know, the onUserInteraction()
is simply not called while the user is interacting with a dialog (even started from Activity
in which you're monitoring interactions).
Two solutions I know are:
Subclass Dialog
/DialogPreference
class and override dispatchTouchEvent()
.
Implement Window.Callback
interface and set it as Dialog
s window callback by issuing:
dialog.getWindow().setCallback(callbackImplementation);
Note: this implementation should process all received events by calling appropriate dialog methods or handle the events in your own way (e.g. by manually calling onUserInteraction()
).
Edit
You have couple of ways to get Activity
from the custom PreferenceDialog
instance.
Call DialogPreference.getPreferenceManager()
method which returns PreferenceManager
. It has a getActivity()
method but it's package-private so you would have to put your custom DialogPreference
in android.preference
package to access it.
In the PreferenceActivity.onCreate()
, after inflating the preferences, use findPreference()
to find your custom DialogPreference
by key. Then cast it to your custom class and set activity to this
via an accessor.
I would go with the second option.