androiddialogdialog-preference

onUserInteraction not working in DialogPreference


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.


Solution

  • 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:

    Edit

    You have couple of ways to get Activity from the custom PreferenceDialog instance.

    1. 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.

    2. 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.