0

I have managed to implement a native Android Activity on Flutter. The code looks like this:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Communication extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyCommunication();
  }
}

class MyCommunication extends State<Communication> {
  static const platform = const MethodChannel("test_activity");
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      body: FutureBuilder<Widget>(
          future: getNewActivity(),
          builder: (BuildContext context, AsyncSnapshot<Widget> snapshot) {
            if (snapshot.hasData)
              return snapshot.data;
            else
              return Container(child: CircularProgressIndicator());
          }),
    ));
  }

  Future<Widget> getNewActivity() async {
    try {
      return await platform.invokeMethod('startNewActivity');
    } on PlatformException catch (e) {
      print(e.message);
    }
  }
}

The problem is, when I tap the back button from the native activity to go to the screen that presented it, the app displays the Container with a circular progress indicator returned when snapshot.hasData is false.

I have to tap again on Back to dismiss this screen and get to the intended presenting view. Obviously there is something I am not doing right in the code. How could I fix this?

YU No
  • 69
  • 1
  • 7

1 Answers1

0

Becuase when you open startNewActivity, now there are 2 screens in stack, and so onBackPressed only top of it removed last one is still there you should use finish() in startNewActivity in Native end to close FlutterPage

theshivamlko
  • 271
  • 2
  • 10
  • Tried overriding onBackPressed() and calling finish() inside but the Container view still appears on the stack :( – YU No Jun 05 '20 at 18:28
  • no, not on backpress, call finish with CLEAR_STACK when you open new Activity, so it will close the flutter page, if you dont want flutterscreen after calling `startNewActivity` – theshivamlko Jun 05 '20 at 19:12