Before doing a factory reset, connect your phone to Wi-Fi and a power source. Then use "iCloud backup" to back up your iOS device, or "Google account backup" to backup your Android device.