5

I'm creating an activity that looks like a dialog.

Here is the style:

<style name="TablesDialogActivity" parent="@android:style/Theme.Holo.Dialog">
    <item name="android:windowBackground">@drawable/frame_background_left</item>
</style>

Here is the activity onCreate():

protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(LayoutParams.FLAG_NOT_TOUCH_MODAL, LayoutParams.FLAG_NOT_TOUCH_MODAL);
        getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
        super.onCreate(savedInstanceState);
}

And also inside the activity touch interceptor:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
        finish();
        return true;
    }
    return false;
}

It pretty much works, activity finishes on touch outside dialog bounds, but it also interacts with buttons on the background activity which is bad. Documentation on the onTouchEvent says that you should return true if you consumed the touch event. I return true, but it doesn't seem that way.

Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157
  • http://stackoverflow.com/questions/4650246/how-to-cancel-an-dialog-themed-like-activity-when-touched-outside-the-window/5831214#5831214 it's good answer) – xoxol_89 Apr 17 '14 at 07:24
  • have you looked on my code? if you did you'd know it's taken from there. – Jacek Kwiecień Apr 17 '14 at 07:45
  • You can find my solution to this problem here: http://stackoverflow.com/questions/16701915/can-we-interact-with-background-activity-when-displaying-a-dialog-over-it-in-and – user2288580 Jul 29 '15 at 08:16

2 Answers2

0

hm..interesting)
in my app i use fragments, so i use DialogFragment instead of Dialog.
i created safe show dialog method

private static void showDialog(FragmentManager fragmentManager, String dialogTag, BeamDialogData data) {

        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.commit();
        fragmentManager.executePendingTransactions();
        Fragment prev = fragmentManager.findFragmentByTag(dialogTag);
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        BeamDialog beamDialog = new BeamDialog();
        beamDialog.setData(data);
        beamDialog.show(fragmentManager, dialogTag);
    }

    public static void showDialogSafe(final FragmentManager fragmentManager, final String dialogTag, 
            final BeamDialogData data, Handler handler) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                    showDialog(fragmentManager, dialogTag, data);
            }           
        });     
    }

BeamDialog is my custom DialogFragment
so there are not backgrounds clicks) i hope, that this will useful for you)

xoxol_89
  • 1,242
  • 11
  • 17
0

Note, that you will need the whole code from the question too.

In addition to my solution inside background activity (or just base activity of your application) I added:

private FrameLayout touchInterceptor;

@Override
protected void onPause() {
    if (touchInterceptor.getParent() == null) {
        ((ViewGroup) findViewById(android.R.id.content)).addView(touchInterceptor);
    }
    super.onPause();
}

@Override
protected void onResume() {
    ((ViewGroup) findViewById(android.R.id.content)).removeView(touchInterceptor);
    super.onResume();
}

And in the onCreate():

    // For intercepting clicks from dialog like activities
    touchInterceptor = new FrameLayout(this);
    touchInterceptor.setClickable(true);

Now works like a charm! :)

Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157