Skip to content

AppStateEventNotifier.appStateStream not working on Android #1436

@AppTyrant

Description

@AppTyrant

[REQUIRED] Step 1: Describe your environment

Plugin Version

8.0.0

[REQUIRED] Step 2: Describe the problem

AppStateEventNotifier isn't triggering lifecycle events on Android, but it is working fine on iOS.
I am planning on using this event to present an AppOpenAd at the appropriate time. I have this bit of code:

await AppStateEventNotifier.startListening();
_appStateSubscription = AppStateEventNotifier.appStateStream.listen((state) {
if (state != AppState.foreground) {
return;
}
// Do whatever here
});

And if I set a breakpoint right on if (state != AppState.foreground) I never hit it when I foreground/background the app on the Android emulator. On iOS this works fine. It looks like it is related to the code in AppStateNotifier.java, which is:

@Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    switch (call.method) {
      case "start":
        start();
        break;
      case "stop":
        stop();
        break;
      default:
        result.notImplemented();
    }
  }

According to docs here "Handler implementations must submit a result for all incoming calls, by making a single call on the given MethodChannel.Result callback. Failure to do so will result in lingering Flutter result handlers. The result may be submitted asynchronously and on any thread. Calls to unknown or unimplemented methods should be handled using MethodChannel.Result.notImplemented()."

I believe this needs to be:

@Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    switch (call.method) {
      case "start":
        start();
        result.success(null);
        break;
      case "stop":
        stop();
       result.success(null);
        break;
      default:
        result.notImplemented();
    }
  }

If I make those changes in the AppStateNotifier.java I get the expected results on Android. My Android emulator is running Android version 16.

Steps to Reproduce

  1. Use GoogleMobileAds flutter plugin.
  2. Use AppStateEventNotifier.appStateStream to listen for app state changes.
  3. Set a breakpoint inside the onData function.
  4. Run the app on iOS, foreground and background it while attached to the debugger and the breakpoint is hit.
  5. On Android do the same thing and the breakpoint is not hit.

Expected results:
AppStateEventNotifier.appStateStream would post notifications for changes in AppState on both iOS and Android.

Actual results:
This only is working on iOS.

Metadata

Metadata

Assignees

Labels

platform-androidAndroid applications specifically

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions