Prevent “Dialog” Activity from interacting with background activity when clicking outside bounds

无人久伴 提交于 2019-12-05 08:27:06

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)

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! :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!