Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to know if app is closed or not? #473

Open
unleed-l opened this issue Aug 23, 2024 · 3 comments
Open

Is there a way to know if app is closed or not? #473

unleed-l opened this issue Aug 23, 2024 · 3 comments

Comments

@unleed-l
Copy link

I need to run two tasks in the background, one needs to run even if the app is closed and the other cannot run if the app is closed. Is there a way to do this?

@Henrikkee
Copy link
Contributor

You can implement the WidgetsBindingObserver interface to check if the app is in the resumed state or in the foreground before executing each task.

@unleed-l
Copy link
Author

unleed-l commented Sep 2, 2024

i tryed this, but it does not work when the app isn't resumed and you close the app from the recent apps.

@unleed-l
Copy link
Author

unleed-l commented Sep 2, 2024

But i already solved it creating a plugin to listen for app lifecycle using native code.

class AppStateCheckerPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
  private lateinit var channel : MethodChannel
  private var activity: Activity? = null
  private lateinit var sharedPreferences: SharedPreferences

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    channel = MethodChannel(flutterPluginBinding.binaryMessenger, "app_state_checker")
    channel.setMethodCallHandler(this)
    sharedPreferences = flutterPluginBinding.applicationContext.getSharedPreferences("AppStatePrefs", Context.MODE_PRIVATE)
  }

  override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    when (call.method) {
      "isAppOpen" -> {
        val isAppOpen = sharedPreferences.getBoolean("isAppOpen", false)
        result.success(isAppOpen)
      }
      else -> result.notImplemented()
    }
  }

  override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    activity = binding.activity
    setAppState(true)  // App foi aberto
  }

  override fun onDetachedFromActivityForConfigChanges() {
    setAppState(false)  // App foi fechado
  }

  override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    activity = binding.activity
    setAppState(true)  // App foi reaberto
  }

  override fun onDetachedFromActivity() {
    setAppState(false)  // App foi fechado
  }

  private fun setAppState(isOpen: Boolean) {
    sharedPreferences.edit().putBoolean("isAppOpen", isOpen).apply()
  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    channel.setMethodCallHandler(null)
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants