How To Open Java Android Activity in flutter

Viewed 35

How can I open an android activity from a flutter widget here is what i currently have in the flutter widget

static const myChannel = MethodChannel('get_image');
ElevatedButton(onPressed: () {
myChannel.invokeMethod('get_image');},
child: const Text('Take Picture'),

And in my mainActivity.java

import android.content.Intent;
import android.os.Bundle;
import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {}

I don,t have much idea on how to move from here most of the answers i have seen in are written in kotlin including the documemtation in flutter website both how can i use this in Java

1 Answers

Try this...

In MainActivity.java (in my case .kt)

class MainActivity : FlutterActivity()  {
    private val CHANNEL = "nativeChannel"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
                .setMethodCallHandler { call: MethodCall, result ->

                    when (call.method) {                     
                        "setToast" -> {
                            try{
                               val myText = call.argument<String>("myText")!! 
                               setText(myText) 
                            } catch (e: Exception){}
                            result.success(true)
                        }
                       
                  
                    }

                }
    }

    private fun setText(myText: String) {
           Toast.makeText(this, myText, Toast.LENGTH_SHORT).show()
    }


}

In flutter :

const platformMethodChannel = MethodChannel('nativeChannel');
await platformMethodChannel.invokeMethod('setToast', {
          'myText':'This text will show in toast by native channel',
        });

The explanation :

In flutter I create the methodChannel and call the method setToast. For this method I pass the property myText.

In Android MainActivity I create the native channel and write the method for call natives method.

Inside de method configureFlutterEngine I will call all natives methods. So, when the flutter send 'setToast' the method recieve the argument myText. This argument will send for method setToast.

This is the simplest approche that I Know and that I use.

Edit... Add Java Code :

MainActivity.java 

public class MainActivity extends FlutterActivity {
  private static final String CHANNEL = "nativeChannel";

  @Override
  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
  super.configureFlutterEngine(flutterEngine);
    new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
        .setMethodCallHandler(
          (call, result) -> {
              if (call.method.equals("setToast")) {
                 String myText = call.argument("myText");
                 setText(myText); 
              }
          }
        );


   public final setText(String myText) {
         Toast.makeText(this, myText).show();

   }
}
Related