首页 > 解决方案 > Native Android App中的颤振,后按不起作用

问题描述

我阅读了很多关于此的主题,但没有任何效果,并且与我的上下文完全相同。

我正在尝试在原生 Android 项目中添加颤动视图,我有一个原生 Activity 可以启动颤动片段。这个颤振片段包含 2 个视图(firstScreen 和 secondScreen)。FirstScreen 在按下按钮时重定向到 SecondScreen,并且 secondScreen 在按下按钮时弹回第一个屏幕。到现在为止一切正常。

但是,当我在第二个屏幕上时,我想在按下设备后退按钮时返回 firstScreen,但颤动 Fragment 已关闭,我又回到了本机 Activity。

当我单独运行颤振模块时,后面的过程看起来不错。

以下是一些截图:

在此处输入图像描述

这里有一些代码:

在本机 MainActivity

override fun startFlutterModule() {
    val newFlutterFragment: FlutterFragment = FlutterFragment.createDefault()
    supportFragmentManager.beginTransaction()
        .replace(
            fragmentContainerId,
            newFlutterFragment,
            TAG_FLUTTER_FRAGMENT
        )
        .addToBackStack(TAG_FLUTTER_FRAGMENT)
        .commit()
}

基本颤振应用

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      routes: {
        '/first': (context) => FirstScreen(),
        '/second': (context) => SecondScreen(),
      },
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: FirstScreen(),
    );
  }
}

第一个颤振屏幕

class FirstScreen extends StatefulWidget {
  @override
  _FirstScreenState createState() => _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              "First Screen",
              style: TextStyle(
                fontSize: 30.0,
                fontWeight: FontWeight.w600,
              ),
            ),
            Padding(
              padding: EdgeInsets.all(10.0),
              child: RaisedButton(
                padding: EdgeInsets.symmetric(
                  horizontal: 20.0,
                  vertical: 10.0,
                ),
                onPressed: (){
                  Navigator.pushNamed(context, '/second');
                },
                child: Text(
                  "Screen 2",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 20.0,
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

第二个颤动屏幕

class SecondScreen extends StatefulWidget {
  @override
  _SecondScreenState createState() => _SecondScreenState();
}

class _SecondScreenState extends State<SecondScreen> {
  @override
  Widget build(BuildContext context) {
    return new WillPopScope(
        onWillPop: () async {
          Navigator.pop(context);
          return Future.value(false);
        },
        child: Scaffold(
          backgroundColor: Colors.white,
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  "Second Screen",
                  style: TextStyle(
                    fontSize: 30.0,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                Padding(
                  padding: EdgeInsets.all(10.0),
                  child: RaisedButton(
                    padding: EdgeInsets.symmetric(
                      horizontal: 20.0,
                      vertical: 10.0,
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    child: Text(
                      "Screen 1",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 20.0,
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
        )
    );
  }
}

标签: androidfluttermobilenavigationonbackpressed

解决方案


从您的本机活动调用 FlutterFragment 的 onBackPressed() 方法。

 override fun onBackPressed() {

    _flutterFragment.onBackPressed()
 }

推荐阅读