I think this might be more complicated than it needs to be.  What if you just do

  oldInputMethod.deactivate()
  newInputMethod.activate(...)

(But test to be sure those functions are defined, of course)

With the current code, it looks like if the new input method does not use a candidate panel then it will never be activated