Chosen Solution
I replaced the screen couple days ago. I tested the new screen before closing the phone and putting the last 2 screws, the screen worked perfectly, the 3d touch and the camera, all was fine. As soon as I closed it, the phone started turning on and off constantly. I notice is the exact moment when the earpiece makes contact with the screen. If somebody found an answer to this issue will help me a lot.
This usually happens when either the home button or front facing camera flex have a small tear in them. It mostly occurs on the home button flex. That would be the reason it works while open. As when it is shut it makes the trace where the tear might be to separate. It is best to examine the flexes with a magnifying glass as the tears are hard to detect with the naked eye.
This is a setting in iPhone. When it detects you have your phone to your ear, it will turn the screen off until it realises you’ve moved it away. It’s basically to save power. It’s with the proximity sensor. Do this: Restart your iPhone Check if any objects are in the way