function at onStateChanged