If..else to back button

Appling an if..then..else to the back button for best code practice and
avoid future errors.