Android screen flash when removeView

535 views Asked by At

I have an activity and several fragments. For only one fragment, I want to disable the screenshot function. In onCreate function, I set flags for the window, but I found this doesn't work. Maybe system needs reload the window. So I try to remove current window and then add it back. This does work, but I find the screen will be black for one second. How can I solve this screen flash problem? This is the onCreate for the fragment. I can't set flag in Activity since that will disable the screenshot for all the fragments.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActivity().getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
        Window window = getActivity().getWindow();
        WindowManager wm = getActivity().getWindowManager();
        wm.removeViewImmediate(window.getDecorView());
        wm.addView(window.getDecorView(), window.getAttributes());

    }
}
1

There are 1 answers

0
Tim Mutton On

I believe you are correct that you need to set that flag before the setContentView(...) on the activity, otherwise one thing you can try is using getActivity().recreate() after setting FLAG_SECURE