Flutter 和 Android原生(Activity、Fragment)相互跳转、传参

前言

本文主要讲解 Flutter 和 Android原生之间,页面相互跳转、传参,

但其中用到了两端相互通信的知识,非常建议先看完这篇 讲解通信的文章

Flutter 与 Android原生 相互通信:BasicMessageChannel、MethodChannel、EventChannel_flutter eventchannel methodchannel basemessagechan-CSDN博客

当前案例 Flutter SDK版本:3.13.2

Flutter使用多个轻量型引擎,进行混合开发,是从 2.0 开始的,大大的减轻了内存压力;

轻量型引擎开发-官方介绍视频:https://www.youtube.com/watch?v=p6cK_0jp2ag

Flutter和原生端的关系

混合路由栈

如果是纯Flutter开发只会有一个Flutter引擎,如果是混合开发,原生端 需要为每个从原生端跳转的Flutter页面创建独立的引擎 (也可以单例,但单例写法需要处理一些问题,这个到 FlutterEngineGroup 章节会具体讲解);

比如:

1.0 从 Android_A页面  ==== 跳转到 ==== Flutter_A页面,Android端需要为Flutter_A页面,创建Flutter引擎;

1.1 紧接着,从 Flutter_A页面 ==== 跳转到 ==== Flutter_B页面,就不需要,它俩共用一个引擎;

1.2 每个Flutter引擎都有自己的路由栈,且这个路由栈只能管理Flutter页面

1.3 使用Flutter提供的 Navigator.pop(context); 方法,可以从 Flutter_B页面 回退到 Flutter_A页面,但无法从 Flutter_A页面 回退到 Android_A,会黑屏,因为Flutter栈里面没有Android页面,可以使用 Navigator.canPop(context); 来检查Flutter路由栈中,是否还有其他路由;

1.4 如果不使用 Navigator.pop(context); 回退方法,使用手机自带的 Back按键 / 左滑屏幕 进行回退,是没有问题的,因为这种回退方式调用的是原生API,Android原生不光提供FlutterView渲染Flutter页面结果,还会创建FlutterActivityFlutterView进行绑定;

1.5 看到这,大家应该理解,为什么说Flutter只是UI框架了吧;

Android 和 Flutter 跳转

Android 跳转 Flutter

Flutter 跳转 Android

效果图

Android代码

FlutterRouterManager.kt

package com.example.flutter_nav_android.util

import android.app.Activity
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterActivityLaunchConfigs
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor

class FlutterRouterManager(
    val targetRoute: String,
    val mEngineId: String,
    val mContext: Activity
) {

    var mEngine: FlutterEngine? = null

    init {
        createCachedEngine()
    }

    companion object {

        /**
         * 获取缓存中的 Flutter引擎
         */
        @JvmStatic
        fun getEngineCacheInstance(engineId: String): FlutterEngine? {
            return FlutterEngineCache.getInstance().get(engineId)
        }

    }

    /**
     * 1、创建Flutter引擎
     * 2、将初始命名路由,修改为目标页面路由
     * 3、缓存Flutter引擎
     */
    fun createCachedEngine(): FlutterEngine {
        val flutterEngine = FlutterEngine(mContext) // 创建Flutter引擎

        // 将初始命名路由,修改为目标页面路由
        flutterEngine.navigationChannel.setInitialRoute(targetRoute)

        // 这一步,是在执行相关Dart文件入口的 main函数,将Flutter页面渲染出结果
        // 原生端获取结果,进行最终渲染上屏
        flutterEngine.dartExecutor.executeDartEntrypoint(
            DartExecutor.DartEntrypoint.createDefault()
        )

        // 将加载好的引擎,存储起来
        FlutterEngineCache.getInstance().put(mEngineId, flutterEngine)

        mEngine = flutterEngine

        return flutterEngine
    }

    /**
     * 根据引擎ID,前往指定的Flutter页面
     */
    fun push() {
        // 创建新的引擎(了解即可)
        // mContext.startActivity(
        //    FlutterActivity
        //        .withNewEngine() // 创建引擎
        //        .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent) // 背景改为透明,不然切换页面时,会闪烁黑色
        //        .build(mContext))

        // 使用缓存好的引擎(推荐)
        mContext.startActivity(
            FlutterActivity
                .withCachedEngine(mEngineId) // 获取缓存好的引擎
                .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent) // 背景改为透明,不然切换页面时,会闪烁黑色
                .build(mContext)
        )
    }

    /**
     * 销毁当前Flutter引擎
     */
    fun destroy() {
        mEngine?.destroy()
    }

}

PersonalActivity.kt

package com.example.flutter_nav_android.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.example.flutter_nav_android.databinding.ActivityPersonalBinding
import com.example.flutter_nav_android.util.FlutterRouterManager
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

class PersonalActivity : AppCompatActivity(), MethodChannel.MethodCallHandler, View.OnClickListener {

    private lateinit var bind: ActivityPersonalBinding

    private lateinit var homeFlutterEngine: FlutterEngine
    private lateinit var loginRouterManager: FlutterRouterManager

    private lateinit var loginMethodChannel: MethodChannel
    private lateinit var homeMethodChannel: MethodChannel

    private val METHOD_CHANNEL_LOGIN = "com.example.flutter_nav_android/login/method"
    private val METHOD_CHANNEL_HOME = "com.example.flutter_nav_android/home/method"
    private val NAV_FLUTTER_LOGIN_NOTICE = "navFlutterLoginNotice"
    private val POP_NOTICE = "popNotice"
    private val PERSONAL_POP_NOTICE = "personalPopNotice"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bind = ActivityPersonalBinding.inflate(layoutInflater)
        setContentView(bind.root)
        initView()

        loginRouterManager = FlutterRouterManager("/login", "login_engine", this)

        // 两端建立通信
        loginMethodChannel = MethodChannel(loginRouterManager.mEngine!!.dartExecutor, METHOD_CHANNEL_LOGIN)
        loginMethodChannel.setMethodCallHandler(this)

        // 获取 Flutter Home页面的引擎,并且建立通信
        homeFlutterEngine = FlutterRouterManager.getEngineCacheInstance("home_engine")!!
        homeMethodChannel = MethodChannel(homeFlutterEngine.dartExecutor,METHOD_CHANNEL_HOME)
    }

    /**
     * 监听来自 Flutter端 的消息通道
     *
     * call: Android端 接收到 Flutter端 发来的 数据对象
     * result:Android端 给 Flutter端 执行回调的接口对象
     */
    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        val methodName: String = call.method
        when (methodName) { // 销毁 Flutter Login 页面
            POP_NOTICE -> {
                val age = call.argument<Int>("age")
                getResult(age.toString())
                loginRouterManager.mEngine!!.navigationChannel.popRoute()
            }
            else -> {
                result.notImplemented()
            }
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            bind.toFlutter -> { // 前往 Flutter Login 页面
                val map: MutableMap<String, String> = mutableMapOf<String, String>()
                map["name"] = "老王"
                loginMethodChannel.invokeMethod(NAV_FLUTTER_LOGIN_NOTICE,map)
                loginRouterManager.push()
            }
            bind.pop -> { // 销毁 Android Personal 页面
                val map: MutableMap<String, Int> = mutableMapOf<String, Int>()
                map["age"] = 18
                homeMethodChannel.invokeMethod(PERSONAL_POP_NOTICE,map)
                finish()
            }
        }
    }

    /**
     * 初始化页面
     */
    private fun initView() {
        bind.toFlutter.setOnClickListener(this)
        bind.pop.setOnClickListener(this)

        var name = intent.getStringExtra("name")
        val title = "接收初始化参数:"
        val msg = title + name

        val ss = SpannableString(msg)
        ss.setSpan(
            ForegroundColorSpan(Color.RED),
            title.length,
            msg.length,
            Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        )
        bind.initV.text = ss
    }

    /**
     * 获取上一页的返回参数
     */
    private fun getResult(age: String) {
        val title = "接收上一页返回参数:"
        val msg = title + age

        val ss = SpannableString(msg)
        ss.setSpan(
            ForegroundColorSpan(Color.RED),
            title.length,
            msg.length,
            Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        )
        bind.resultV.text = ss
    }

    override fun onDestroy() {
        super.onDestroy()
        loginRouterManager.destroy()
    }

}

SchoolActivity.kt

package com.example.flutter_nav_android.ui.activity

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.flutter_nav_android.databinding.ActivitySchoolBinding
import com.example.flutter_nav_android.util.FlutterRouterManager
import io.flutter.embedding.android.FlutterFragment
import io.flutter.embedding.android.TransparencyMode
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.plugin.common.MethodChannel


class SchoolActivity : AppCompatActivity() {

    private lateinit var bind: ActivitySchoolBinding
    private lateinit var bookFragment: FlutterFragment
    private lateinit var studentFragment: FlutterFragment

    private val METHOD_CHANNEL_BOOK = "com.example.flutter_nav_android/book/method"
    private val METHOD_CHANNEL_STUDENT = "com.example.flutter_nav_android/student/method"

    private val NAV_FLUTTER_BOOK_NOTICE = "navFlutterBookNotice"
    private val NAV_FLUTTER_STUDENT_NOTICE = "navFlutterStudentNotice"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bind = ActivitySchoolBinding.inflate(layoutInflater)
        setContentView(bind.root)
        initView()
        initChannel()
    }

    /**
     * 建立通信
     */
    private fun initChannel() {
        val bookEngine = FlutterRouterManager.getEngineCacheInstance("book_engine")
        val bookChannel = MethodChannel(bookEngine!!.dartExecutor,METHOD_CHANNEL_BOOK)
        val map: MutableMap<String, String> = mutableMapOf<String, String>()
        map["title"] = "Book"
        bookChannel.invokeMethod(NAV_FLUTTER_BOOK_NOTICE,map)

        val studentEngine = FlutterRouterManager.getEngineCacheInstance("student_engine")
        val studentChannel = MethodChannel(studentEngine!!.dartExecutor,METHOD_CHANNEL_STUDENT)
        val map2: MutableMap<String, String> = mutableMapOf<String, String>()
        map2["title"] = "Student"
        studentChannel.invokeMethod(NAV_FLUTTER_STUDENT_NOTICE,map2)
    }

    /**
     * 初始化页面
     */
    private fun initView() {
        bookFragment = FlutterFragment.withCachedEngine("book_engine")
            .transparencyMode(TransparencyMode.transparent) // 背景透明,避免切换页面,出现闪烁
            .shouldAttachEngineToActivity(false) // 是否让Flutter控制Activity,true:可以 false:不可以,默认值 true
            .build()

        supportFragmentManager
            .beginTransaction()
            .add(bind.bookFragment.id, bookFragment)
            .commit()

        studentFragment = FlutterFragment.withCachedEngine("student_engine")
            .transparencyMode(TransparencyMode.transparent) // 背景透明,避免切换页面,出现闪烁
            .shouldAttachEngineToActivity(false) // 是否让Flutter控制Activity,true:可以 false:不可以,默认值 true
            .build()

        supportFragmentManager
            .beginTransaction()
            .add(bind.studentFragment.id, studentFragment)
            .commit()
    }

    // ================================ 这些是固定写法,Flutter需要这些回调 ================================

    override fun onPostResume() {
        super.onPostResume()
        bookFragment.onPostResume()
        studentFragment.onPostResume()
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        bookFragment.onNewIntent(intent)
        studentFragment.onNewIntent(intent)
    }

    override fun onBackPressed() {
        bookFragment.onBackPressed()
        studentFragment.onBackPressed()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        bookFragment.onRequestPermissionsResult(
            requestCode,
            permissions,
            grantResults
        )
        studentFragment.onRequestPermissionsResult(
            requestCode,
            permissions,
            grantResults
        )
    }

    override fun onUserLeaveHint() {
        bookFragment.onUserLeaveHint()
        studentFragment.onUserLeaveHint()
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        bookFragment.onTrimMemory(level)
        studentFragment.onTrimMemory(level)
    }

}

MainActivity.kt

package com.example.flutter_nav_android.ui.activity

import android.content.Intent
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.example.flutter_nav_android.databinding.ActivityMainBinding
import com.example.flutter_nav_android.util.FlutterRouterManager
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel


class MainActivity : AppCompatActivity(), MethodChannel.MethodCallHandler, View.OnClickListener {

    private lateinit var bind: ActivityMainBinding

    private lateinit var homeMethodChannel: MethodChannel

    private val METHOD_CHANNEL_HOME = "com.example.flutter_nav_android/home/method"
    private val NAV_ANDROID_PERSONAL_NOTICE = "navAndroidPersonalNotice"
    private val NAV_FLUTTER_HOME_NOTICE = "navFlutterHomeNotice"
    private val POP_NOTICE = "popNotice"

    private lateinit var homeRouterManager: FlutterRouterManager
    private lateinit var bookRouterManager: FlutterRouterManager
    private lateinit var studentRouterManager: FlutterRouterManager

    override fun onCreate(savedInstanceState: android.os.Bundle?) {
        super.onCreate(savedInstanceState)
        bind = ActivityMainBinding.inflate(layoutInflater)
        setContentView(bind.root)
        bind.toFlutter.setOnClickListener(this)
        bind.toFlutterFragment.setOnClickListener(this)

        homeRouterManager = FlutterRouterManager("/home", "home_engine", this)

        // 两端建立通信
        homeMethodChannel = MethodChannel(homeRouterManager.mEngine!!.dartExecutor,METHOD_CHANNEL_HOME)
        homeMethodChannel.setMethodCallHandler(this)

        // 这里Fragment案例的
        bookRouterManager = FlutterRouterManager("/book", "book_engine", this)
        studentRouterManager = FlutterRouterManager("/student", "student_engine", this)
    }

    /**
     * 监听来自 Flutter端 的消息通道
     *
     * call: Android端 接收到 Flutter端 发来的 数据对象
     * result:Android端 给 Flutter端 执行回调的接口对象
     */
    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        val methodName: String = call.method
        when (methodName) {
            NAV_ANDROID_PERSONAL_NOTICE -> { // Flutter Home 页面 前往 Android Personal 页面
                val intent = Intent(this, PersonalActivity::class.java)
                intent.putExtra("name",call.argument<String>("name"))
                startActivity(intent)
            }
            POP_NOTICE -> { // 销毁 Flutter Home 页面
                val age = call.argument<Int>("age")
                getResult(age.toString())
                homeRouterManager.mEngine!!.navigationChannel.popRoute()
            }
            else -> {
                result.notImplemented()
            }
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            bind.toFlutter -> { // 前往 Flutter Home 页面
                val map: MutableMap<String, String> = mutableMapOf<String, String>()
                map["name"] = "张三"
                homeMethodChannel.invokeMethod(NAV_FLUTTER_HOME_NOTICE,map)
                homeRouterManager.push()
            }
            bind.toFlutterFragment -> {
                val intent = Intent(this, SchoolActivity::class.java)
                startActivity(intent)
            }
        }
    }

    /**
     * 获取上一页的返回参数
     */
    private fun getResult(age: String) {
        val title = "接收上一页返回参数:"
        val msg = title + age

        val ss = SpannableString(msg)
        ss.setSpan(
            ForegroundColorSpan(Color.RED),
            title.length,
            msg.length,
            Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        )
        bind.resultV.text = ss
    }

    override fun onDestroy() {
        super.onDestroy()
        homeRouterManager.destroy()
        bookRouterManager.destroy()
        studentRouterManager.destroy()
    }

}

activity_personal.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginBottom="16dp"
            android:background="@color/cardview_shadow_start_color"
            android:gravity="center_vertical"
            android:paddingStart="16dp"
            android:text="Android Personal"
            android:textColor="@color/material_dynamic_primary60"
            android:textSize="26sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/init_v"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="接收初始化参数:"
            android:textColor="@color/material_dynamic_primary60"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/result_v"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="接收上一页返回参数:"
            android:textColor="@color/material_dynamic_primary60"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/init_v" />

        <Button
            android:id="@+id/to_flutter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="前往 Flutter Login"
            android:textSize="20sp"
            android:textAllCaps="false"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/result_v" />

        <Button
            android:id="@+id/pop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="返回 上一页"
            android:textSize="20sp"
            android:textAllCaps="false"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/to_flutter" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

activity_school.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="20dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@color/cardview_shadow_start_color"
            android:gravity="center_vertical"
            android:paddingStart="16dp"
            android:text="Android School"
            android:textColor="@color/material_dynamic_primary60"
            android:textSize="26sp"
            android:textStyle="bold" />

        <FrameLayout
            android:id="@+id/book_fragment"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp" />

        <FrameLayout
            android:id="@+id/student_fragment"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp" />

    </LinearLayout>
</layout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginBottom="16dp"
            android:background="@color/cardview_shadow_start_color"
            android:gravity="center_vertical"
            android:paddingStart="16dp"
            android:text="Android Main"
            android:textColor="@color/material_dynamic_primary60"
            android:textSize="26sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/result_v"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="接收上一页返回参数:"
            android:textColor="@color/material_dynamic_primary60"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

        <Button
            android:id="@+id/to_flutter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="前往 Flutter Home"
            android:textSize="20sp"
            android:textAllCaps="false"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/result_v"
            app:layout_constraintRight_toRightOf="parent" />

        <Button
            android:id="@+id/to_flutter_fragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="前往 Flutter Fragment"
            android:textSize="20sp"
            android:textAllCaps="false"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/to_flutter"
            app:layout_constraintRight_toRightOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">


    <application
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher"
        android:label="flutter_nav_android"
        android:theme="@style/AppTheme">

        <activity
            android:name="io.flutter.embedding.android.FlutterActivity"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize" />

        <activity android:name=".ui.activity.SchoolActivity" android:exported="true"/>

        <activity android:name=".ui.activity.PersonalActivity" android:exported="true"/>

        <activity
            android:name=".ui.activity.MainActivity"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:exported="true"
            android:hardwareAccelerated="true"
            android:launchMode="singleTop"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
                android:name="io.flutter.embedding.android.NormalTheme"
                android:resource="@style/NormalTheme" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
    <!-- Required to query activities that can process text, see:
         https://developer.android.com/training/package-visibility?hl=en and
         https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.

         In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT" />
            <data android:mimeType="text/plain" />
        </intent>
    </queries>
</manifest>

Flutter代码

book.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Book extends StatefulWidget {
  const Book({super.key});

  @override
  State<Book> createState() => _BookState();
}

class _BookState extends State<Book> {

  String title = '';

  static const String METHOD_CHANNEL_BOOK = 'com.example.flutter_nav_android/book/method';
  static const String NAV_FLUTTER_BOOK_NOTICE = 'navFlutterBookNotice';

  @override
  void initState() {
    super.initState();
    MethodChannel bookMethodChannel = const MethodChannel(METHOD_CHANNEL_BOOK);
    bookMethodChannel.setMethodCallHandler(methodHandler);
  }

  /// 监听来自 Android端 的消息通道
  /// Android端调用了函数,这个handler函数就会被触发
  Future<dynamic> methodHandler(MethodCall call) async {
    final String methodName = call.method;
    switch (methodName) {
      case NAV_FLUTTER_BOOK_NOTICE: // 进入当前页面
        {
          title = call.arguments['title'];
          setState(() {});
          return 0;
        }
      default:
        {
          return PlatformException(
              code: '-1',
              message: '未找到Flutter端具体实现函数',
              details: '具体描述'); // 返回给Android端
        }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.amberAccent,
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        alignment: Alignment.center,
        child: Text(
          'Flutter $title',
          style: const TextStyle(
            fontWeight: FontWeight.bold,
            color: Colors.red,
            fontSize: 20,
          ),
        ),
      ),
    );
  }

}

home.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  late MethodChannel homeMethodChannel;

  String name = '';
  String age = '';

  static const String METHOD_CHANNEL_HOME = 'com.example.flutter_nav_android/home/method';
  static const String NAV_ANDROID_PERSONAL_NOTICE = 'navAndroidPersonalNotice';
  static const String NAV_FLUTTER_HOME_NOTICE = 'navFlutterHomeNotice';
  static const String POP_NOTICE = 'popNotice';
  static const String PERSONAL_POP_NOTICE = 'personalPopNotice';

  @override
  void initState() {
    super.initState();
    homeMethodChannel = const MethodChannel(METHOD_CHANNEL_HOME);
    homeMethodChannel.setMethodCallHandler(methodHandler);
  }

  /// 监听来自 Android端 的消息通道
  /// Android端调用了函数,这个handler函数就会被触发
  Future<dynamic> methodHandler(MethodCall call) async {
    final String methodName = call.method;
    switch (methodName) {
      case NAV_FLUTTER_HOME_NOTICE: // 进入当前页面
        {
          name = call.arguments['name'];
          setState(() {});
          return 0;
        }
      case PERSONAL_POP_NOTICE: // Android Personal 页面 销毁了
        {
          age = '${call.arguments['age']}';
          setState(() {});
          return 0;
        }
      default:
        {
          return PlatformException(
              code: '-1',
              message: '未找到Flutter端具体实现函数',
              details: '具体描述'); // 返回给Android端
        }
    }
  }

  /// 销毁当前页面
  popPage() {
    if (Navigator.canPop(context)) { // 检查Flutter路由栈中,是否还有其他路由
      Navigator.pop(context);
    } else {
      Map<String, int> map = {'age': 12};
      homeMethodChannel.invokeMethod(POP_NOTICE, map);
    }
  }

  /// 前往 Android Personal 页面
  navAndroidPersonal() {
    Map<String, String> map = {'name': '李四'};
    homeMethodChannel.invokeMethod(NAV_ANDROID_PERSONAL_NOTICE, map);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          backgroundColor: Colors.blue,
          title: const Text(
            'Flutter Home',
            style: TextStyle(
                fontWeight: FontWeight.w500,
                fontSize: 26,
                color: Colors.yellow),
          )),
      body: SizedBox(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.only(bottom: 16),
              child: RichText(
                  text: TextSpan(
                      text: '接收初始化参数:',
                      style: const TextStyle(color: Colors.black, fontSize: 20),
                      children: [
                    TextSpan(
                      text: name,
                      style: const TextStyle(color: Colors.red,fontWeight: FontWeight.bold),
                    )
                  ])),
            ),
            Padding(
              padding: const EdgeInsets.only(bottom: 16),
              child: RichText(
                  text: TextSpan(
                      text: '接收上一页返回参数:',
                      style: const TextStyle(color: Colors.black, fontSize: 20),
                      children: [
                    TextSpan(
                      text: age,
                      style: const TextStyle(color: Colors.red,fontWeight: FontWeight.bold),
                    )
                  ])),
            ),
            Padding(
              padding: const EdgeInsets.only(bottom: 8),
              child: ElevatedButton(
                onPressed: navAndroidPersonal,
                child: const Text(
                  '前往 Android Personal',
                  style: TextStyle(fontSize: 20),
                ),
              ),
            ),
            ElevatedButton(
              onPressed: popPage,
              child: const Text(
                '返回 上一页',
                style: TextStyle(fontSize: 20),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

login.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Login extends StatefulWidget {
  const Login({super.key});

  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
  late MethodChannel loginMethodChannel;

  String name = '';

  final String METHOD_CHANNEL_LOGIN = 'com.example.flutter_nav_android/login/method';
  static const String NAV_FLUTTER_LOGIN_NOTICE = 'navFlutterLoginNotice';
  final String POP_NOTICE = 'popNotice';

  @override
  void initState() {
    super.initState();
    loginMethodChannel = MethodChannel(METHOD_CHANNEL_LOGIN);
    loginMethodChannel.setMethodCallHandler(methodHandler);
  }

  /// 监听来自 Android端 的消息通道
  /// Android端调用了函数,这个handler函数就会被触发
  Future<dynamic> methodHandler(MethodCall call) async {
    final String methodName = call.method;
    switch (methodName) {
      case NAV_FLUTTER_LOGIN_NOTICE: // 进入当前页面
        {
          name = call.arguments['name'];
          setState(() {});
          return 0;
        }
      default:
        {
          return PlatformException(code: '-1', message: '未找到Flutter端具体实现函数', details: '具体描述'); // 返回给Android端
        }
    }
  }

  /// 销毁当前页面
  popPage() {
    if (Navigator.canPop(context)) { // 检查Flutter路由栈中,是否还有其他路由
      Navigator.pop(context);
    } else {
      Map<String, int> map = {'age': 28};
      loginMethodChannel.invokeMethod(POP_NOTICE, map);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          backgroundColor: Colors.blue,
          title: const Text(
            'Flutter Login',
            style: TextStyle(
                fontWeight: FontWeight.w500,
                fontSize: 26,
                color: Colors.yellow),
          )),
      body: SizedBox(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.only(bottom: 16),
              child: RichText(
                  text: TextSpan(
                      text: '接收初始化参数:',
                      style: const TextStyle(color: Colors.black, fontSize: 20),
                      children: [
                    TextSpan(
                      text: name,
                      style: const TextStyle(color: Colors.red,fontWeight: FontWeight.bold),
                    )
                  ])),
            ),
            ElevatedButton(
              onPressed: popPage,
              child: const Text(
                '返回 上一页',
                style: TextStyle(fontSize: 20),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

student.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Student extends StatefulWidget {
  const Student({super.key});

  @override
  State<Student> createState() => _StudentState();
}

class _StudentState extends State<Student> {

  String title = '';

  static const String METHOD_CHANNEL_STUDENT = 'com.example.flutter_nav_android/student/method';
  static const String NAV_FLUTTER_STUDENT_NOTICE = 'navFlutterStudentNotice';

  @override
  void initState() {
    super.initState();
    MethodChannel bookMethodChannel = const MethodChannel(METHOD_CHANNEL_STUDENT);
    bookMethodChannel.setMethodCallHandler(methodHandler);
  }

  /// 监听来自 Android端 的消息通道
  /// Android端调用了函数,这个handler函数就会被触发
  Future<dynamic> methodHandler(MethodCall call) async {
    final String methodName = call.method;
    switch (methodName) {
      case NAV_FLUTTER_STUDENT_NOTICE: // 进入当前页面
        {
          title = call.arguments['title'];
          setState(() {});
          return 0;
        }
      default:
        {
          return PlatformException(
              code: '-1',
              message: '未找到Flutter端具体实现函数',
              details: '具体描述'); // 返回给Android端
        }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.cyan,
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        alignment: Alignment.center,
        child: Text(
          'Flutter $title',
          style: const TextStyle(
            fontWeight: FontWeight.bold,
            color: Colors.white,
            fontSize: 20,
          ),
        ),
      ),
    );
  }

}

main.dart

import 'package:flutter/material.dart';
import 'package:flutter_nav_android/book.dart';
import 'package:flutter_nav_android/student.dart';

import 'home.dart';
import 'login.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
            seedColor: Colors.deepPurple,
         ),
        useMaterial3: true,
      ),
      routes: {
        '/home': (context) => const Home(),
        '/login': (context) => const Login(),
        '/book': (context) => const Book(),
        '/student': (context) => const Student(),
      },
      initialRoute: '/home',
    );
  }
}

踩坑

  • 一个路由坑,Flutter 加载 根命名路由标识,默认是 " / ",如果你在Flutter端使用了 " / " 作为根路由页面,有时候,从Android 跳转 Flutter页面时,它会将 根路由页面 先push进栈,再push你的目标页面进栈,会多出一个页面
  • 所有我直接不用 " / " 标识,而使用页面对应的路由标识字符串,来指定根路由。
// 原来带坑的写法
// routes: {
//    '/': (context) => const Home(),
//    '/login': (context) => const Login(),
//    '/personal': (context) => const Personal()
// },
// initialRoute: '/',


// 解决方式的写法
routes: {
    '/home': (context) => const Home(),
    '/login': (context) => const Login(),
    '/personal': (context) => const Personal()
 },
initialRoute: '/home',

奇技淫巧

在案例中,我使用的是 命名路由Channel方式 进行 页面跳转、传参,下面直接使用引擎进行 页面跳转、传参,但实用价值不高,因为弊端太大;

比如:

  • 只提供了List<String>类型,进行传参;
  • 目标Flutter页面内的widget,默认参数会全部失效;
  • Flutter的Widget默认参数,是由主文件内的 MaterialApp 组件提供的,一个Flutter应用,一般只会使用一个 MaterialApp,它代表返回一个App,如果将目标Flutter页面套上 MaterialApp ,可以解决这个默认参数问题,但会引发 路由问题
  • 不过也有适用场景:让每个Flutter页面完全独立,之间没有任何交互,比如跳转、数据共享等等,这样路由就不需要了,让每个Flutter页面都使用 MaterialApp,当作App和原生交互;

综上所述,大家将这种方式当作扩展知识就好了。

Android代码

class MainActivity : AppCompatActivity(), View.OnClickListener {

    ... ... 

    override fun onCreate(savedInstanceState: android.os.Bundle?) {
        super.onCreate(savedInstanceState)

        ... ... 

        val flutterEngine = FlutterEngine(this)

        // 定义参数
        val dartEntrypointArgs = mutableListOf<String>()
        dartEntrypointArgs.add("张三")

        // 这种方式传参数,会导致这个Flutter页面中的Widget默认参数,全部失效,

        // 这种只有 Dart主入口文件,比如main.dart的 main函数才能接收到参数
        // void main(List<String args>) {}
        // flutterEngine.dartExecutor.executeDartEntrypoint(
        //    DartExecutor.DartEntrypoint.createDefault(),
        //    dartEntrypointArgs
        // )

        // 这种可以指定页面接收,但需要在主入口文件里先声明       
        // void showPersonal(List<String> args) {}
        flutterEngine.dartExecutor.executeDartEntrypoint(
            DartExecutor.DartEntrypoint(
                FlutterInjector.instance().flutterLoader().findAppBundlePath(),
                "showPersonal"), // 找到目标Flutter页面提供的 暴露函数
            dartEntrypointArgs)
        FlutterEngineCache.getInstance().put("personal_engine", flutterEngine)
    }


    override fun onClick(v: View?) {
        ... ... 

        val map: MutableMap<String, String> = mutableMapOf<String, String>()
        map["name"] = "张三"
        startActivity(
            FlutterActivity
                .withCachedEngine("personal_engine") // 获取缓存好的引擎
                .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent) // 背景改为透明,不然切换页面时,会闪烁黑色
                .build(this))
    }

}

Flutter代码

main.dart(主文件)

import 'package:flutter/material.dart';
import 'package:flutter_nav_android/personal.dart';

void main(List<String> args) {
  debugPrint('args:$args');
  runApp(const MyApp());
}

/// 注解说明文档:https://mrale.ph/dartvm/compiler/aot/entry_point_pragma.html
/// 注解:表明它可以在 AOT 模式下直接从本机或 VM 代码解析、分配或调用
@pragma("vm:entry-point")
void showPersonal(List<String> args) { // 在主文件入口暴露出来
  debugPrint('args:$args');
  runApp(Personal(title: args.first));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
            seedColor: Colors.deepPurple,
         ),
        useMaterial3: true,
      ),
    );
  }
}

personal.dart(目标页面)

import 'package:flutter/material.dart';

class Personal extends StatefulWidget {
  final String? title;
  const Personal({super.key,this.title});

  @override
  State<Personal> createState() => _PersonalState();
}

class _PersonalState extends State<Personal> {
  
  /// 在这个页面中,使用的Widget 默认参数全部失效

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      alignment: Alignment.center,
      child: Directionality(
          textDirection: TextDirection.ltr,
          child: Text(
            widget.title ?? '',
            style: const TextStyle(
              color: Colors.lightBlue,
              fontSize: 30,
            ),
          )),
    );
  }
}

FlutterEngineGroup

  • 如果你接受以上缺点,可以使用 FlutterEngineGroup,这个东西就是换成了单例写法,我们自己写单例,需要处理一个问题,比如这个需求:同时获取两个Flutter引擎,这里就要通过判空新建一个Flutter引擎;
  • 而 FlutterEngineGroup 已经处理了这个问题,自首个Flutter引擎起,后面多出的Flutter引擎,都是其子类,如果只剩下最后一个,那么这个Flutter引擎将和首个Flutter引擎性能特征相同;
  • FlutterEngineGroup官方文档:多个 Flutter 页面或视图 - Flutter 中文文档 - Flutter 中文开发者网站 - Flutter
  • 我个人体验上来说,不使用单例,性能也足够了,如果有很多混合的Flutter页面,那当我没说😐。
  • 没找到创建子类Flutter引擎的相关代码,是从注解中发现的

Debug 和 Release

我用真机做测试,发现在Debug模式下,第一次从 Android 跳转 Flutter 会出现黑屏现象,但 打完包 或在 Release模式 就看不出来了;

我们默认的运行模式就是 Debug模式,可以通过 修改IDE运行命令 --release,切换为 Release模式

源码地址

GitHub - LanSeLianMa/flutter_nav_android: Flutter 和 Android原生(Activity、Fragment)相互跳转、传参

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/369757.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

MongoDB复制集实战及原理分析

文章目录 MongoDB复制集复制集架构三节点复制集模式PSS模式&#xff08;官方推荐模式&#xff09;PSA模式 典型三节点复制集环境搭建复制集注意事项环境准备配置复制集复制集状态查询使用mtools创建复制集安全认证复制集连接方式 复制集成员角色属性一&#xff1a;Priority 0属…

match-case与if/elif/else(python)

if/elif/else语句应对一般场景&#xff0c;match-case主打复杂条件分支语句。 (笔记模板由python脚本于2024年01月28日 18:27:37创建&#xff0c;本篇笔记适合有一定编程基础&#xff0c;对python基础已比较扎实的coder翻阅) 【学习的细节是欢悦的历程】 Python 官网&#xff1…

uniapp使用u-popup组件弹窗出现页面还可滑动

*1、问题所在&#xff1a; 弹窗遮罩层出现了页面依旧可以上下滑动 2、要求: 为了用户更好交互体验&#xff0c;弹窗出现后应禁止页面往下滑动 3、实现思路&#xff1a; 在弹窗盒子外层添加个阻止触摸冒泡事件&#xff0c;使用touchmove.stop.prevent 4、代码如下&#xff…

eosio.token 智能合约介绍

一、目的 eosio.token系统合约定义了允许用户为基于EOSIO的区块链创建、发行和管理代币的结构和操作&#xff0c;它演示了一种实现允许创建和管理代币的智能合约的方法。本文详细介绍了eosio.token系统合约并在本地测试链上实际发行了代币进行演示&#xff0c;适用于EOS智能合…

OJ刷题:《剑指offer》之单身狗1、2 !(巧用位操作符,超详细讲解!)

目录 1.单身狗1 1.1 题目描述 1.2排序寻找 1.3巧用位操作符 2.单身狗2 1.1 题目描述 1.2排序寻找 1.3巧用位操作符 不是每个人都能做自己想做的事&#xff0c;成为自己想成为的人。 克心守己&#xff0c;律己则安&#xff01; 创作不易&#xff0c;宝子们&#xff01;如…

homework day3

第三章 类与构造函数 一&#xff0e;选择题 1、下列不能作为类的成员的是&#xff08;B&#xff09; A. 自身类对象的指针 B. 自身类对象 C. 自身类对象的引用 D. 另一个类的对象 2、假定AA为一个类&#xff0c;a()为该类公有的函数成员&#xff0c;x为该类的一个对象&am…

如何在一台MacBook上构建大模型知识库?

▼最近直播超级多&#xff0c;预约保你有收获 今晚直播&#xff1a;《构建大模型知识库案例实战》 —1— 如何在一台 MacBook 上构建企业知识库&#xff1f; 最核心最重要的是我们手上的文档资料出于安全要求&#xff0c;不能随便上传到云服务&#xff0c;也就无法实际验证知识…

单链表的经典题目练习

哈喽&#xff0c;小伙伴们&#xff0c;上一次我们学习了单链表的知识&#xff0c;这次我们就要运用学到的知识来做一些相关的题目。我们都知道&#xff0c;要学好数据结构与算法&#xff0c;一定要多刷相关的题目才能有所提高。所以我们一起来学习一些单链表的经典题目算法题。…

操作系统透视:从历史沿革到现代应用,剖析Linux与网站服务架构

目录 操作系统 windows macos Linux 服务器搭建网站 关于解释器的流程 curl -I命令 名词解释 dos bash/terminal&#xff0c;(终端) nginx/apache&#xff08;Linux平台下的&#xff09; iis&#xff08;Windows平台下的&#xff09; GUI(图形化管理接口&#xff…

Multisim14.0仿真(四十九)共阴极/阳极7段数码管驱动设计

一、74LS47/48简介: 74LS47/48芯片是一种常用的七段数码管译码器驱动器,常用在各种数字电路和单片机系统的显示系统中. 二、74LS47/48引脚说明及定义: 7段显示译码器74LS47/48是输出低/高电平有效的译码器,74LS47/48除了有实现7段显示译码器基本功能的输入(DCBA)和输出(Ya…

小程序<swiper/>组件详解及使用指南

目录 引言微信小程序的重要性Swiper组件的角色与功能简介Swiper组件基础Swiper组件的定义与使用场景如何在微信小程序中引入Swiper组件Swiper组件的基本结构与属性Swiper组件的高级应用自定义Swiper指示点样式实现Swiper的动态效果(如自动播放、循环播放)说明引言 微信小程序…

时序预测 | MATLAB实现基于CNN-BiLSTM-AdaBoost卷积双向长短期记忆网络结合AdaBoost时间序列预测

时序预测 | MATLAB实现基于CNN-BiLSTM-AdaBoost卷积双向长短期记忆网络结合AdaBoost时间序列预测 目录 时序预测 | MATLAB实现基于CNN-BiLSTM-AdaBoost卷积双向长短期记忆网络结合AdaBoost时间序列预测预测效果基本介绍模型描述程序设计参考资料 预测效果 基本介绍 1.Matlab实现…

【C++】类和对象之运算符重载(三)

前言&#xff1a;在前面我们知道在类和对象中有六个默认成员函数&#xff0c;并学习了其中三个构造函数、析构函数、拷贝构造函数&#xff0c;今天我们将进一步的学习.赋值运算符重载。 &#x1f496; 博主CSDN主页:卫卫卫的个人主页 &#x1f49e; &#x1f449; 专栏分类:高质…

[SWPUCTF 2021 新生赛]ez_unserialize

根据下面的user_agent和Disallow可以判断这个是在robots.txt 我们看的出来这是一个反序列化需要我们adminadmin passwdctf construct 构造方法&#xff0c;当一个对象被创建时调用此方法&#xff0c;不过unserialize()时却不会被调用 destruct 析构方法&#xff0c;PHP将在对象…

【学网攻】 第(20)节 -- 网络端口地址转换NAPT配置

系列文章目录 目录 系列文章目录 文章目录 前言 一、NAPT是什么&#xff1f; 二、实验 1.引入 实验目的 技术原理 实验步骤 实验设备 实验拓扑图 实验配置 实验验证 文章目录 【学网攻】 第(1)节 -- 认识网络【学网攻】 第(2)节 -- 交换机认识及使用【学网攻】 第…

JavaGUI之SWT框架【阶段练习】

文章目录 效果展示选项卡界面创建划分右侧区域填充右侧上方Composite填充右侧下方Composite填充左侧Composite完整代码 SWT基础部分的内容以全部写完&#xff0c;现在让我们将以前学到的知识综合到一起&#xff0c;写一个小demo&#xff08;无交互功能&#xff09; 效果展示 选…

『运维备忘录』之 Systemd 命令详解

运维人员不仅要熟悉操作系统、服务器、网络等只是&#xff0c;甚至对于开发相关的也要有所了解。很多运维工作者可能一时半会记不住那么多命令、代码、方法、原理或者用法等等。这里我将结合自身工作&#xff0c;持续给大家更新运维工作所需要接触到的知识点&#xff0c;希望大…

Java实现批量视频抽帧2.0

继上个版本 对其进行略微升级 &#x1f913; 上个版本仅对一个视频进行抽帧处理 此版本可对一个文件夹内的全部视频进行抽帧并对应的文件夹进行帧图片的保存 1️⃣配置pom.xml &#xff08;保持上次不变&#xff09; <dependencies><dependency><grou…

Jmeter组件执行顺序与作用域(超详细整理)

&#x1f345; 视频学习&#xff1a;文末有免费的配套视频可观看 &#x1f345; 点击文末小卡片 &#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;薪资嘎嘎涨 一、Jmeter重要组件 1&#xff09;配置元件---Config Element&#xff1a; 用于初始化默认值…

缓存组件Caffeine的使用

caffeine是一个高性能的缓存组件&#xff0c;在需要缓存数据&#xff0c;但数据量不算太大&#xff0c;不想引入redis的时候&#xff0c;caffeine就是一个不错的选择。可以把caffeine理解为一个简单的redis。 1、导入依赖 <!-- https://mvnrepository.com/artifact/com.git…