flutter_background_service icon indicating copy to clipboard operation
flutter_background_service copied to clipboard

service.stopSelf(); Killing and Exiting Whole Application

Open EXEIdeas opened this issue 2 years ago • 3 comments

I am using it and it is working fine. Here is my flutter doctor.

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.10.5, on macOS 12.4 21F79 darwin-arm, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] Connected device (2 available)
[✓] HTTP Host Availability
• No issues found!

I started this service on home/dashboard/main screen using the below code.

FlutterBackgroundService().startService().then((value) {
      print("Session Started On App Load.");
});

Then I invoke/on some functions and everything worked fine. Now at some time, I want to close the running service from App UI button click. So on a button, I wrote the below code.

FlutterBackgroundService().invoke('stopService');

And in background service function, I have the below code...

void onStart(ServiceInstance service) {
  WidgetsFlutterBinding.ensureInitialized();

  if (service is AndroidServiceInstance) {
    print("Background Service Started");

    service.setAsForegroundService();

    service.on('setAsForeground').listen((event) {
      service.setAsForegroundService();
    });
    service.on('setAsBackground').listen((event) {
      service.setAsBackgroundService();
    });
    service.on(actionStopService).listen((event) {
      service.stopSelf();     // Killing/Stopping The Whole App
    });
    service.on('someCustomAction').listen((event) {
      print('Custom Action');
    });
}

Question is why? I never created another instance of FlutterBackgroundService() in whole app except first screen as mentioned earlier. So why it is happening...???

EXEIdeas avatar Jun 03 '22 10:06 EXEIdeas

@EXEIdeas could you provide the crash log?

ekasetiawans avatar Jun 03 '22 11:06 ekasetiawans

flutter run -v First try: https://anotepad.com/notes/iqwpnyp6 Another try: https://anotepad.com/notes/twahc576

EXEIdeas avatar Jun 03 '22 11:06 EXEIdeas

@EXEIdeas I found this message in your log provided

+2875 ms] D/AndroidRuntime(27684): Shutting down VM
[   +3 ms] E/AndroidRuntime(27684): FATAL EXCEPTION: main
[        ] E/AndroidRuntime(27684): Process: com.novusguardsolo, PID: 27684
[        ] E/AndroidRuntime(27684): java.lang.RuntimeException: Unable to destroy activity {com.novusguardsolo/com.example.novus_guard_solo.MainActivity}:
kotlin.UninitializedPropertyAccessException: lateinit property sensorEventListener has not been initialized
[        ] E/AndroidRuntime(27684):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5454)
[        ] E/AndroidRuntime(27684):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5487)
[        ] E/AndroidRuntime(27684):     at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:47)
[        ] E/AndroidRuntime(27684):     at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
[        ] E/AndroidRuntime(27684):     at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
[        ] E/AndroidRuntime(27684):     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
[        ] E/AndroidRuntime(27684):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2253)
[        ] E/AndroidRuntime(27684):     at android.os.Handler.dispatchMessage(Handler.java:106)
[        ] E/AndroidRuntime(27684):     at android.os.Looper.loopOnce(Looper.java:201)
[        ] E/AndroidRuntime(27684):     at android.os.Looper.loop(Looper.java:288)

And the crash seems not caused by flutter_background_service plugin. So, you have to trace every plugin you used.

ekasetiawans avatar Jun 03 '22 12:06 ekasetiawans