Seems to me that the only way to retain the benefit - for yourself and potentially also for others - of data generated by the app using the old phone is to keep that old phone active for two weeks after you switch over.
Obviously switch the SIM card over to the new one, but keep that old phone connected to Wi-Fi, keep it charged up and keep checking it, so that, over the course of the next 14 days*, the app can still advise you of any contact or venue alerts relating to two week period before you switched phones.
And also so that, if you test positive yourself within that time, you can register that in the app so that any other users who received beacons from your old phone can be notified accordingly.
And only erase the old phone after those 14 days have passed (after which time all codes on your phone - and your codes on others' phones - will have been deleted automatically).
In the meantime, of course, also set up the app on the new phone that you're now taking with you everywhere, to provide ongoing tracking.
*These timings relate to the NHS COVID-19 app for England and Wales. Don't know whether they might differ for other apps and regions.