How to add 2nd Android activity in Flutter?

451 views Asked by At

I am currently integrating a google tap&pay SDK in my Flutter app. My app startActivityForResult the native Google wallet Activity to add a card to wallet. Now wallet Activity wants to startActivityForResult another activity in my app to get the verification code.

Now since all Flutter apps based on single activity architecture. If I start a new instance of the same Activity in my flutter app the session gets invalidated for the previous instance. So I cannot work with multiple instances of the same Activity.

How can I add another Activity in my Flutter app which will could be launched and share the same Flutter engine?

The diagram looks something like this:

A --> B --> C

  1. A: Flutter Activity
  2. B: Google Wallet Activity
  3. C: Another Activity for getting verification code
2

There are 2 answers

1
aminjafari-dev On

I think you can use flutter_isolate package

Flutter isolate

0
Zibran On

To add a second Android activity in your Flutter app, you can follow these steps:

  1. Create a new Android Activity: You can create a new Android activity in the android/app/src/main/java/your/package/name directory. This activity will be used to get the verification code.
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class SecondActivity extends FlutterActivity {
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}
  1. Add the new Activity to AndroidManifest.xml: You need to declare this activity in your AndroidManifest.xml file which is located in android/app/src/main directory.
<activity android:name=".SecondActivity" >
    <intent-filter>
        <!-- Intent filters if any -->
    </intent-filter>
</activity>
  1. Launch the new Activity from Flutter: You can use MethodChannel to communicate between Flutter and native Android. Here is an example of how you can do this:
MethodChannel channel = MethodChannel('com.example/second_activity');
channel.invokeMethod('startSecondActivity');

And in your SecondActivity.java:

new MethodChannel(getFlutterView(), "com.example/second_activity").setMethodCallHandler(
  new MethodCallHandler() {
    @Override
    public void onMethodCall(MethodCall call, Result result) {
      if (call.method.equals("startSecondActivity")) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
      }
    }
  });

Please note that you need to replace com.example with your own package name.

  1. Share the same FlutterEngine: The primary API for adding multiple Flutter instances on both Android and iOS is based on a new FlutterEngineGroup class. This allows you to share the same Flutter engine across multiple activities.

Remember, while this approach allows you to add a second activity to your Flutter app, it's generally recommended to use a single activity architecture in Flutter apps when possible. Multiple activities can lead to increased complexity and potential performance issues.