terminate_restart 1.0.0
terminate_restart: ^1.0.0 copied to clipboard
A robust Flutter plugin for terminating and restarting your app with extensive customization options. Supports both Android and iOS with features like data clearing and state preservation.
๐ Terminate Restart #
A robust Flutter plugin for terminating and restarting your app with extensive customization options. Perfect for implementing dynamic updates, clearing app state, or refreshing your app's UI.
๐ Features #
- โจ Two Restart Modes:
- UI-only restart (recreate activities/views)
- Full process termination and restart
- ๐งน Smart Data Management:
- Optional data clearing during restart
- Configurable data preservation options
- Proper cleanup of app state
- ๐ Secure Data Handling:
- Preserve keychain data
- Preserve user defaults/shared preferences
- Clean process termination
- ๐ฑ Platform Support:
- โ Android
- โ iOS (coming soon)
- ๐จ Rich UI Options:
- Customizable confirmation dialogs
- Immediate mode for quick restarts
- Beautiful default UI
- โก Performance:
- Minimal initialization delay
- Optimized process handling
- Clean state management
๐ฆ Installation #
Add this to your package's pubspec.yaml
file:
dependencies:
terminate_restart: ^1.0.0
๐ Usage #
Basic Usage #
import 'package:terminate_restart/terminate_restart.dart';
// Simple UI-only restart
await TerminateRestart.restartApp(
terminate: false, // false for UI-only restart
);
// Full process termination and restart
await TerminateRestart.restartApp(
terminate: true, // true for full process termination
);
Advanced Usage with Data Clearing #
// Restart with data clearing
await TerminateRestart.restartApp(
clearData: true, // Clear app data
preserveKeychain: true, // Keep sensitive data
preserveUserDefaults: false, // Clear preferences
terminate: true, // Full process restart
);
Confirmation Dialog #
// Restart with custom confirmation dialog
await TerminateRestart.restartApp(
context: context, // Required for dialog
mode: RestartMode.withConfirmation,
dialogTitle: 'โจ Update Ready!',
dialogMessage: 'Restart now to apply updates?',
restartNowText: '๐ Restart Now',
restartLaterText: 'โฐ Later',
cancelText: 'โ Cancel',
);
Error Handling #
try {
final success = await TerminateRestart.restartApp(
terminate: true,
clearData: true,
);
if (!success) {
print('Restart cancelled or failed');
}
} catch (e) {
print('Error during restart: $e');
}
Complete Example with State Management #
class _MyHomePageState extends State<MyHomePage> {
// Store persistent data
late SharedPreferences _prefs;
int _counter = 0;
@override
void initState() {
super.initState();
_loadCounter();
}
Future<void> _loadCounter() async {
_prefs = await SharedPreferences.getInstance();
setState(() {
_counter = _prefs.getInt('counter') ?? 0;
});
}
Future<void> _incrementCounter() async {
setState(() {
_counter++;
});
await _prefs.setInt('counter', _counter);
}
Future<void> _restartApp() async {
final bool confirmed = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: Text('Restart App'),
content: Text('Do you want to clear data and restart?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: Text('Yes, Restart'),
),
],
),
) ?? false;
if (confirmed) {
await TerminateRestart.restartApp(
clearData: true,
preserveKeychain: true,
terminate: true,
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Terminate Restart Demo')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Counter: $_counter'),
ElevatedButton(
onPressed: _incrementCounter,
child: Text('Increment'),
),
ElevatedButton(
onPressed: _restartApp,
child: Text('Clear & Restart'),
),
],
),
),
);
}
}
๐ API Reference #
TerminateRestart.restartApp() #
Main method to restart your app with various options:
Parameter | Type | Default | Description |
---|---|---|---|
context | BuildContext? | null | Required for confirmation dialog |
mode | RestartMode | immediate | Choose between immediate/confirmation |
clearData | bool | false | Whether to clear app data |
preserveKeychain | bool | false | Keep keychain data when clearing |
preserveUserDefaults | bool | false | Keep user defaults when clearing |
terminate | bool | true | Full process termination vs UI refresh |
dialogTitle | String? | null | Custom title for confirmation dialog |
dialogMessage | String? | null | Custom message for confirmation dialog |
restartNowText | String? | null | Custom text for restart now button |
restartLaterText | String? | null | Custom text for restart later button |
cancelText | String? | null | Custom text for cancel button |
Returns Future<bool>
indicating success or failure.
๐ง Platform-Specific Details #
Android #
- Uses
Process.killProcess()
for clean termination - Proper activity stack handling with Intent flags
- Smart SharedPreferences management
- Handles all app data directories
iOS (Coming Soon) #
- Clean process termination
- State preservation options
- Keychain data handling
- User defaults management
๐ค Contributing #
- Fork it
- Create your feature branch (
git checkout -b feature/amazing
) - Commit your changes (
git commit -am 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing
) - Create a Pull Request
๐ License #
This project is licensed under the MIT License - see the LICENSE file for details.
๐จโ๐ป Author #
Made with โค๏ธ by Ahmed Sleem