4

Android activity has overridden method onUserInteraction.But how can I check the user interaction in the dialog?I want to develop some logic based on this.Please help This code works (onUserInteraction)fine with Activity public class MyBaseActivity extends Activity {

public static final long DISCONNECT_TIMEOUT = 300000; // 5 min = 5 * 60 * 1000 ms

private Handler disconnectHandler = new Handler(){
    public void handleMessage(Message msg) {
    }
};

private Runnable disconnectCallback = new Runnable() {
    @Override
    public void run() {
        // Perform any required operation on disconnect
    }
};

public void resetDisconnectTimer(){
    disconnectHandler.removeCallbacks(disconnectCallback);
    disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
}

public void stopDisconnectTimer(){
    disconnectHandler.removeCallbacks(disconnectCallback);
}

@Override
public void onUserInteraction(){
    resetDisconnectTimer();
}

@Override
public void onResume() {
    super.onResume();
    resetDisconnectTimer();
}

@Override
public void onStop() {
    super.onStop();
    stopDisconnectTimer();
}

}

But how can I use 'onUserInteraction' method with Dialog ?

button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            final Dialog dialog = new Dialog(MainActivity.this);

            //setting custom layout to dialog
            dialog.setContentView(R.layout.cusotm_dialog_layout);
            dialog.setTitle("Custom Dialog");

            //adding text dynamically
            TextView txt = (TextView) dialog.findViewById(R.id.textView);
            txt.setText("Put your dialog text here.");

            ImageView image = (ImageView)dialog.findViewById(R.id.image);
            image.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_dialog_info));

            //adding button click event
            Button dismissButton = (Button) dialog.findViewById(R.id.button);
            dismissButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        }

1 Answers1

-1

I have implemented onUserInteraction for DialogFragment. This has the same handling as Activity for onUserInteraction. The setUserInteractionEnabled method can also be implemented in an Activity subclass.

public abstract class BaseDialogFragment
extends DialogFragment
{    
    protected void onUserInteraction()
    {
        Activity activity = getActivity();
        if(activity != null)
        {
            activity.onUserInteraction();
        }
    }

    protected void setUserInteractionEnabled(Dialog dialog, boolean enabled)
    {
        if(!enabled)
        {
            dialog.getWindow().setCallback(dialog);
            return;
        }

        dialog.getWindow().setCallback(new WindowCallbackWrapper(dialog)
        {
            @Override
            public boolean dispatchKeyEvent(KeyEvent event)
            {
                onUserInteraction();
                return super.dispatchKeyEvent(event);
            }

            @Override
            public boolean dispatchKeyShortcutEvent(KeyEvent event)
            {
                onUserInteraction();
                return super.dispatchKeyShortcutEvent(event);
            }

            @Override
            public boolean dispatchTouchEvent(MotionEvent event)
            {
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    onUserInteraction();
                }
                return super.dispatchTouchEvent(event);
            }

            @Override
            public boolean dispatchTrackballEvent(MotionEvent event)
            {
                onUserInteraction();
                return super.dispatchTrackballEvent(event);
            }

            @Override
            public boolean dispatchGenericMotionEvent(MotionEvent event)
            {
                onUserInteraction();
                return super.dispatchGenericMotionEvent(event);
            }
        });
    }
}

Call this in your onCreateDialog.

public class MyDialogFragment
extends BaseDialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        Dialog dialog = new Dialog(getContext());

        setUserInteractionEnabled(dialog, true);

        return dialog;
    }
}

Note: this will not call onUserInteraction from the dialog's soft keyboard. That should be handled from a TextWatcher that calls onUserInteraction in afterTextChanged.