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.
Here is a complete solution for a
DialogFragment
, which triggers Activity'sonUserInteraction()
on a touch and preserves default callback's behavior:And here is the Callback itself:
As far as I know, the
onUserInteraction()
is simply not called while the user is interacting with a dialog (even started fromActivity
in which you're monitoring interactions).Two solutions I know are:
Subclass
Dialog
/DialogPreference
class and overridedispatchTouchEvent()
.Implement
Window.Callback
interface and set it asDialog
s window callback by issuing: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 customPreferenceDialog
instance.Call
DialogPreference.getPreferenceManager()
method which returnsPreferenceManager
. It has agetActivity()
method but it's package-private so you would have to put your customDialogPreference
inandroid.preference
package to access it.In the
PreferenceActivity.onCreate()
, after inflating the preferences, usefindPreference()
to find your customDialogPreference
by key. Then cast it to your custom class and set activity tothis
via an accessor.I would go with the second option.