2

I use Activity.onUserInteraction() in my application which triggers on any interaction (e.g. tapping the screen somewhere) even if it will not lead to action. Perfect for screen timeouts, etc.

public class MainActivity extends Activity {
    ...
    public void onUserInteraction()
    {
        idleTimer = 15;
    }
    ...
}

However, when I show a dialog (via AlertDialog.Builder) the above method won't be called anymore. I understand that it isn't my activity anymore, but how can I implement something similar when the dialog is active? I would like to track any activity, not just the ones result in action to manage (lower) brightness manually after a given length of user inactivity.

In case you need details about how the dialog is shown:

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    View view = getLayoutInflater().inflate(R.layout.number_picker, null);
    final NumberPicker np = (NumberPicker)view.findViewById(R.id.numberPicker);
    if (np == null)
        return;

    alertDialogBuilder
            .setMessage(title)
            .setView(view)
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    MainActivity.onNumberPickerClose(code, np.getValue());
                    dialog.cancel();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    AlertDialog alertDialog = alertDialogBuilder.create();

    MainActivity.SetupNumberPicker(np, from, to, defaultValue);

    alertDialog.show();
Thomas
  • 816
  • 5
  • 16
  • I guess Its happening because dialog is another/sub activity. So you need to use main activity context in it to fetch touch events – VVB Sep 22 '14 at 07:18
  • Any idea how to "use main activity context"? Anyway, it's a little bit strange that onPause() is not called when the dialog is shown - if it's a different activity, shouldn't it be called? – Thomas Sep 22 '14 at 18:03

0 Answers0