Search code examples
javaandroidandroid-activityandroid-dialogonactivityresult

Android Activity: Listener for outside touch event


I have an Activity shown as a Dialog:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setTheme(android.R.style.Theme_Dialog);
    setFinishOnTouchOutside(true);
}

When the user closes the Activity-Dialog by touching outside the Activity-Dialog window, the Activity finishes.

How can I set a Listener on this event?

This is important because I want to be able to call

setResult(intResultCode, intent);

right before finishing.

Calling setResult() in onPause() can be too late already.


Solution

  • why to struggle that much? just override finish() method in Avtivity...

    @Override
    public void finish() {
        setResult(int resultCode, Intent data);
        super.finish();
    }