Open android(native) activity in
2020-09-02 本文已影响0人
老中医167788
Sep-02-2020 15-47-39.gifPreview
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
}
}