美文网首页Flutter
Open android(native) activity in

Open android(native) activity in

作者: 老中医167788 | 来源:发表于2020-09-02 15:48 被阅读0次

    Preview

    Sep-02-2020 15-47-39.gif

    Flutter:

    RaisedButton(
                  child: Text('Goto Native Page'),
                  onPressed: () {
                    MethodChannel channel = MethodChannel("method_channel_name");
                    channel.invokeMethod('startActivity', {
                      "name" : 'NativePage',
                      "arg" : '123'
                    });
                  },
                ),
    

    Android

    MainActivity.kt

    package com.example.myapp
    
    import android.content.Intent
    import android.os.Bundle
    import io.flutter.app.FlutterActivity
    import io.flutter.plugin.common.MethodCall
    import io.flutter.plugin.common.MethodChannel
    import io.flutter.plugins.GeneratedPluginRegistrant
    
    class MainActivity : FlutterActivity(),MethodChannel.MethodCallHandler{
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            GeneratedPluginRegistrant.registerWith(this)
    
            val channel = MethodChannel(flutterView,"method_channel_name")
            channel.setMethodCallHandler(this::onMethodCall)
        }
    
        override fun onMethodCall(methodCall: MethodCall, result: MethodChannel.Result) {
            if(methodCall.method == "startActivity"){
                val map = methodCall.arguments as HashMap<*, *>
                if(map["name"] == "NativePage"){
                    startActivity(Intent(this,NativeActivity::class.java).apply{
                        addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
                        putExtra("arg",map["arg"].toString())
                    })
                }
            }
        }
    }
    

    NativeActivity.kt

    package com.example.myapp
    
    import android.support.v7.app.AppCompatActivity
    import android.os.Bundle
    import android.view.View
    import android.widget.TextView
    
    class NativeActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_native)
    
            val arg = intent.getStringExtra("arg")
            findViewById<TextView>(R.id.textView).text = arg
        }
    }
    

    相关文章

      网友评论

        本文标题:Open android(native) activity in

        本文链接:https://www.haomeiwen.com/subject/mudisktx.html