iT邦幫忙

2021 iThome 鐵人賽

DAY 21
0
Mobile Development

Flutter - 從 Packages & Plugins 掌握原生系列 第 23

Day23 Plugin 從零開始到上架 - ActivityAware 介紹

  • 分享至 

  • xImage
  •  

ActivityAware

如果插件需要與UI 進行互動,例如請求權限或更改Android UI ,那麼您就需要一些附加步驟來構建您的插件。您必須實現 ActivityAware,Flutter 提供了 ActivityAware 用於 Activity 的生命週期管理和獲取

public class MyPlugin implements FlutterPlugin, ActivityAware {
  //...normal plugin behavior is hidden...

  @Override
  public void onAttachedToActivity(ActivityPluginBinding activityPluginBinding) {
    // TODO: your plugin is now attached to an Activity
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {
    // TODO: the Activity your plugin was attached to was
    // destroyed to change configuration.
    // This call will be followed by onReattachedToActivityForConfigChanges().
  }

  @Override
  public void onReattachedToActivityForConfigChanges(ActivityPluginBinding activityPluginBinding) {
    // TODO: your plugin is now attached to a new Activity
    // after a configuration change.
  }

  @Override
  public void onDetachedFromActivity() {
    // TODO: your plugin is no longer associated with an Activity.
    // Clean up references.
  }
}

onAttachedToEngineonDetachedFromEngineFlutterPlugin的介面方法,是flutter新的載入Plugin 的方式,onAttachedToActivityonDetachedFromActivityActivityAware的介面方法,主要是用於獲取當前flutter 頁面所處的Activity.

我們可以將Plugin 設計將MethodHandleronAttachedToEngine 方法中被初始化和構建,在 onDetachedFromEngine 方法中釋放;同時 Activity 相關的四個實現方法也提供了相應的操作邏輯


上一篇
Day22 Plugin 從零開始到上架 - 取得權杖(iOS)
下一篇
Day24 Plugin 從零開始到上架 - FlutterPlugin與 MethodCallHandler
系列文
Flutter - 從 Packages & Plugins 掌握原生30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言