Dynamically change AppTheme for Android in a fragment using a switch

I have struggled for a bit with this – I am trying to make a Switch widget in the Settings fragment of my app such that when the user clicks on it, he can choose between a Light/Dark color theme. I tried this:

Switch colourThemeSwitch = (Switch) getView().findViewById(R.id.colourThemeSwitch);
    colourThemeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                getContext().getTheme().applyStyle(R.style.DarkTheme, true);
            else {
                getContext().getTheme().applyStyle(R.style.AppTheme, true);

But had no success. After that I tried for debugging purposes to see whether this would work in my onCreateView method, but it doesn’t:


The theme is the same as before. Is what I am trying to achieve even possible? If yes, how would be a recommended way to do it?