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

后端 未结 2 1324
离开以前
离开以前 2021-02-14 11:34

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

         


        
2条回答
  •  深忆病人
    2021-02-14 12:37

    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.

提交回复
热议问题