Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to navigate to a specific flutter route from an Android activity?

I have an existing android application and i have integrated flutter in my project i want to call a flutter specific route which i define in my main method like this

class FlutterView extends StatelessWidget {
 @override
  Widget build(BuildContext context) {
  return new MaterialApp(
  title: 'Platform View',
  initialRoute: '/',
  routes: {
    '/': (context) => HomeScreen(),
    '/secound': (context) => MyCustomForm(),
    '/dashboard': (context) => DashBoardScreen(),
    '/login': (context) => LoginScreen(),
  },
  theme: new ThemeData(
    primarySwatch: Colors.red,
    textSelectionColor: Colors.red,
    textSelectionHandleColor: Colors.red,
    ),
   );
  }
}

from my android activity i am calling flutter activity like this

startActivity(new Intent(this,FlutterActivity.class));

it does open my flutter activity but with the initialRoute: '/' which is fine but some time i want to open for eg( '/dashboard') routes when i open a flutter activity how can i do it ??

like image 302
shakil.k Avatar asked Aug 04 '18 20:08

shakil.k


2 Answers

From Android, as stated here:

Intent intent = new Intent(context, MainActivity.class);
intent.setAction(Intent.ACTION_RUN);
intent.putExtra("route", "/routeName");
context.startActivity(intent);

From Flutter, using android_intent:

AndroidIntent intent = AndroidIntent(
  action: 'android.intent.action.RUN',

  // Replace this by your package name.
  package: 'app.example', 

  // Replace this by your package name followed by the activity you want to open.
  // The default activity provided by Flutter is MainActivity, but you can check
  // this in AndroidManifest.xml.
  componentName: 'app.example.MainActivity', 

  // Replace "routeName" by the route you want to open. Don't forget the "/".
  arguments: {'route': '/routeName'},
);

await intent.launch();

Notice that the app is going to open in this route only if it's terminated, that is, in case the app is in foreground or background, it won't open in the specified route.

like image 164
Hugo Passos Avatar answered Oct 04 '22 17:10

Hugo Passos


Just create a method channel and call a flutter function from Android. In that function navigate your app from flutter to where ever you want.

for more info about how to use method channels to communicate between flutter and native code and vice versa. Please have a look

https://flutter.dev/docs/development/platform-integration/platform-channels

like image 27
Shashank Avatar answered Oct 04 '22 17:10

Shashank