That's why I said I don't understand what the problem is.
I think relay not switching when i connected the battery
True.
The battery has absolutely NOTHING to do with the relay power. The relay energizes when the 5V adaptor is connected and powered up and deenergizes when that adaptor is disconnected or powered down. Whether or not the battery is connected has no bearing on the status of the relay.