Automatically on some devices due to custom Android tweaks done by manufactures. Steps below on disabling this.
- Huawei - Only Pre-EMUI 5.0 / Android 7 - Go to Settings > "Protected apps", check your app. Full Instructions
- Sony - Tap on the battery icon. Go to Power Management > STAMINA mode > Apps active in standby > Add your app.
- Asus - Check your app in the Auto-start Manager.
- Xiaomi - Security (App) > Permissions > Autostart - Enable your app
- *New Xiaomi - Settings > Developer Options. Disable "memory optimization". To enabled Developer Options go to Settings > About. Tap on MIUI 8 times. Full Instructions
- Redmi - Same as Xiaomi
- Oppo - Go to Settings > "Security settings" > "Data saving" and enable your app.
- Samsung - Disable battery usage optimizations
- OnePlus - Go to Settings > Battery > Battery Optimization > Application > Don't Optimize