How to make custom admin “Save and Continue Edit” button work in Magento 2

I recently created a custom module that has a “Save and Continue Edit” button on the admin edit form. The tricky part is to make the page stay on the edit screen after the button is pressed and the form is saved.

When Save and Continue button is pressed, a back/edit string is appended to the submit url by default, so in the save controller we can check for it and set redirect url accordingly.

The return statement of the save controller should look something like this:

    //check for 'back' parameter
    if ($this->getRequest()->getParam('back')) {
        //redirect to edit page
        return $resultRedirect->setPath('*/*/edit', ['id' => $model->getId(), '_current' => true]);
    //redirect to index page
    return $resultRedirect->setPath('*/*/');