In this i am creating an app in which i successfully added the flutter module in existing android app by using these links https://flutter.dev/docs/development/add-to-app , https://flutter.dev/docs/development/add-to-app/android/add-flutter-screen . Here the launch screen is native android and then i added a button to navigate to flutter screen. But my question is how do i navigate from flutter screen to native android screen. Here is the code of native android and dart code. Note: I already added the flutter module dependencies in build.gradle and also added the code in settings.gradle, so i am not going to mention the code.
MainActivity.java
Button btn1;
btn1=findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(FlutterActivity.createDefaultIntent(v.getContext())); //this navigate to flutter page
}
});
Main.dart
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Demo'),),
body: Container(
child: Center(
child: RaisedButton(
onPressed: (){
//Here how do i navigate to Native Android Screen ?
},
child: Text('Navigate to Android'),
),
),
),
);
}
}
In Flutter, you should invoke a method through Platform Channel to call a function to navigate from the activity/view controller which hosts your Flutter, you can't directly navigate to a native view from Flutter.
// .dart file
void _exitFlutter() {
PlatformChannel.invokeMethod('navToNative');
}
// .kt or .java file
MethodChannel(flutterEngine!!.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler { call, result ->
run {
when (call.method) {
"navToNative" -> // start your activity here
else -> result.notImplemented()
}
}
}
You can check the detail here https://github.com/duytq94/demo-integrate-flutter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.