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

SystemNavigator.pop() causes Amplify exception #213

Closed
wasitu opened this issue Nov 25, 2020 · 11 comments
Closed

SystemNavigator.pop() causes Amplify exception #213

wasitu opened this issue Nov 25, 2020 · 11 comments
Labels
duplicate This issue or pull request already exists

Comments

@wasitu
Copy link

wasitu commented Nov 25, 2020

Describe the bug
Flutter's SystemNavigator.pop() causes Amplify exception by starting app again.
(Using android's back button for closing app causes the same problem)

To Reproduce
Steps to reproduce the behavior:

  1. Configure Amplify like
final amplify = Amplify();
amplify.addPlugin(authPlugins: [AmplifyAuthCognito()]);
await amplify.configure('~~~')
  1. Use SystemNavigator.pop() for close app on root page (or press android's back button)
  2. Start app again
  3. You can see error on logcat

Platform
Amplify Flutter current supports iOS and Android. This issue is reproducible in (check all that apply):
[×] Android
[] iOS

Smartphone (please complete the following information):

  • Device: Pixel 4a
  • OS: 11
  • Flutter: 1.22.0 (stable)
@fjnoyp
Copy link
Contributor

fjnoyp commented Nov 26, 2020

Hi @wasitu this issue appears to be related to a previously closed issue - the root cause is from how the Flutter framework behaves:

#99

Please see the workarounds suggested in the issue as a possible solution for your problem:

This seems like an issue where the back button causes the FlutterActivity to be destroyed in Android instead of sending it to background. There are some workarounds mentioned here https://stackoverflow.com/questions/55539302/sending-your-application-to-background-when-back-button-is-pressed-in-flutter that can be used by the applications such as

https://medium.com/stuart-engineering/%EF%B8%8F-the-tricky-task-of-keeping-flutter-running-on-android-2d51bbc60882
https://www.programmersought.com/article/3839440802/

@fjnoyp fjnoyp added Android Issues specific to the Android Platform bug Something is not working; the issue has reproducible steps and has been reproduced core Issues related to the Amplify Core Plugin help wanted duplicate This issue or pull request already exists good first issue Good for newcomers and removed Android Issues specific to the Android Platform core Issues related to the Amplify Core Plugin bug Something is not working; the issue has reproducible steps and has been reproduced help wanted labels Nov 26, 2020
@wasitu
Copy link
Author

wasitu commented Nov 27, 2020

@fjnoyp
Thanks for your reply.

Yeah, I saw those workaround but I think its not that.
Because If I remove Amplify, Everything fine. (Behavior seems to be restarted all and can use other libraries.)

@fjnoyp
Copy link
Contributor

fjnoyp commented Nov 30, 2020

Ah I see @wasitu. Could you provide the full error log you're getting here? Does keeping a bool isConfigured and only calling configure() if it's false fix your problem?

@wasitu
Copy link
Author

wasitu commented Dec 2, 2020

@fjnoyp
Here's log

W/GeneratedPluginsRegister( 9410): Tried to automatically register plugins with FlutterEngine (io.flutter.embedding.engine.FlutterEngine@ecc66fc) but could not find and invoke the GeneratedPluginRegistrant.

E/flutter ( 9410): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method configure on channel com.amazonaws.amplify/core)
E/flutter ( 9410): #0      MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:157
E/flutter ( 9410): <asynchronous suspension>
E/flutter ( 9410): #1      MethodChannel.invokeMethod
package:flutter/…/services/platform_channel.dart:332
E/flutter ( 9410): #2      MethodChannelAmplifyCore.configure
package:amplify_core_plugin_interface/method_channel_amplify.dart:25
E/flutter ( 9410): #3      Amplify.configure
package:amplify_core/amplify_core.dart:71
E/flutter ( 9410): #4      setup
package:myapp/main.dart:47
E/flutter ( 9410): #5      main
package:myapp/main.dart:24
E/flutter ( 9410): #6      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:231:25)
E/flutter ( 9410): #7      _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 9410): #8      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 9410): #9      _runZoned (dart:async/zone.dart:1630:10)
E/flutter ( 9410): #10     runZonedGuarded (dart:async/zone.dart:1618:12)
E/flutter ( 9410): #11     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:223:5)
E/flutter ( 9410): #12     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter ( 9410): #13     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

bool isConfigured can't fix problem. Since Dart seems to be reset, it's always false.
As log said, Automatically registration plugins failed with amplify one.

@wasitu
Copy link
Author

wasitu commented Dec 2, 2020

I fixed it temporally like this.
https://github.com/wasitu/amplify-flutter/commit/80e9eb6ef058e2d8e49ea2b093c489c8739e90fc

But I dont have much time (and smart brain) to test and think if it's ok or not. Just working for me.

@brunovsiqueira
Copy link

I'm having the exact same issue mentioned by @wasitu.

@Rayv1
Copy link

Rayv1 commented Dec 16, 2020

Hi guys,
i had the same Problem and i had to implement the "check-if-amplify-configured"-Fix and the from @fjnoyp mentioned FlutterActivity Workaround from StackOverflow

Its working now independently from how the app is closed.

@brunovsiqueira
Copy link

@Rayv1 I found different solutions in the link you provided. Which solution worked for you?

@Rayv1
Copy link

Rayv1 commented Jan 5, 2021

This Solution worked:
var _androidAppRetain = MethodChannel("android_app_retain");

@override
Widget build(BuildContext context) {
return WillPopScope(
  onWillPop: () {
    if (Platform.isAndroid) {
      if (Navigator.of(context).canPop()) {
        return Future.value(true);
      } else {
        _androidAppRetain.invokeMethod("sendToBackground");
        return Future.value(false);
      }
    } else {
      return Future.value(true);
    }
  },
  child: Scaffold(
    ...
  ),
);

The code in the MainActivity() should look like this:

class MainActivity: FlutterActivity() {

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine);

    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "android_app_retain").apply {
        setMethodCallHandler { method, result ->
            if (method.method == "sendToBackground") {
                moveTaskToBack(true)
            }
        }
    }
}
}

@brunovsiqueira
Copy link

Hi guys. I tried the above solution and it seems to work in the back pressed case but it still causes crashes on Crashlytics.

@Amplifiyer is there any schedule to have this bug fixed? In my opinion it is a really important bug because it crashes the app.

@Ashish-Nanda Ashish-Nanda added help wanted and removed good first issue Good for newcomers wontfix labels Jan 22, 2021
@Amplifiyer
Copy link
Contributor

The original issue with MissingPluginException has been fixed in #345 Please see #99 for more details about this issue. Closing as duplicate. The fix will be available in our next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

6 participants