Creating primitive changes selection
From https://blender.stackexchange.com/a/38626/58482 I found out that I need to set the active object in addition to selecting it. So using bpy.context.scene.objects.active = obj after creating the new object seems to solve the problem.